Pergunta

I am trying to read a URL with file_get_contents(). I need to get only $invoice from the page.

$invoice appears on sentence like the following example as a number 789621 :

<code><div class="show invoice invoice_number_rajhi"><a href="/fatora/alrajhi/invoices/3687908533/people" class="link-blue" onclick="$.fn.colorbox({title:&quot;People who visit this&quot;,onComplete:show.onPeopleOpening,onCleanup:show.onPeopleClosing,href:&‌​quot;/fatora/alrajhi/invoices/3687908533/people&quot;}); return false">789621 paid</a> show this</div></code>

and if it appears as the following example , how could i do it ?

  <code><li class="js-stat-show js-stat-show stat-still"> <a href="#" class="request-invoice-popup" data-activity-popup-title="invoice 789621 paid" > paid <strong>789621</strong> </a> </li></code>
Foi útil?

Solução

Using regular expressions in PHP:

$myurl = "theUrlYouWant";

preg_match("/return false\"\>(.*?) paid/i", file_get_contents($myurl), $matches);

$invoice = $matches[1];

echo($invoice);

Now, the variable $invoice should be equal to the invoice number

Outras dicas

I think you are scraping a webpage, if so, you could use a library like PHP Simple HTML DOM Parser With his you can find tags on an HTML page with selectors just like jQuery.

Then for ex. you could use something like this for getting all the elements.

// Create DOM from URL or file
$html = file_get_html('URL');

// Find all links 
foreach($html->find('a') as $element) 
       echo $element->href . '<br>';
<?

$input = '<code><div class="show invoice invoice_number_rajhi"><a href="/fatora/alrajhi/invoices/3687908533/people" class="link-blue" onclick="$.fn.colorbox({title:&quot;People who visit this&quot;,onComplete:show.onPeopleOpening,onCleanup:show.onPeopleClosing,href:&‌​quot;/fatora/alrajhi/invoices/3687908533/people&quot;}); return false">789621 paid</a> show this</div></code>';

preg_match_all("#(\d*)[ a-z]*?</a>#i",$input,$out);
echo $out[1][0];
?>

You can replace the value of $input with file_get_contents("URL HERE");

Once you have the page you can just scrap it with Javascript or jQuery and get the onclick value that you want by targeting the class name.

you would need a simple_html_dom to scrap the invoice from the page. then use

$ret = $html->find('div[id]');

to retrieve the div that contains the invoice.

$content=$ret->innertext;

gives the content of that div. rest is simple. just split the $content with ' '(space) and get your $invoice

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top