[英]need specific data from a url with file_get_contents ()
我正在嘗試使用file_get_contents()
讀取URL。 我只需要從頁面中獲得$invoice
。
$invoice
出現在句子上,例如以下示例,其編號為789621:
<code><div class="show invoice invoice_number_rajhi"><a href="/fatora/alrajhi/invoices/3687908533/people" class="link-blue" onclick="$.fn.colorbox({title:"People who visit this",onComplete:show.onPeopleOpening,onCleanup:show.onPeopleClosing,href:"/fatora/alrajhi/invoices/3687908533/people"}); return false">789621 paid</a> show this</div></code>
如果出現以下示例,該怎么辦?
<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>
在PHP中使用正則表達式:
$myurl = "theUrlYouWant";
preg_match("/return false\"\>(.*?) paid/i", file_get_contents($myurl), $matches);
$invoice = $matches[1];
echo($invoice);
現在,變量$ invoice應該等於發票編號
我認為您正在抓取網頁,如果是的話,則可以使用PHP Simple HTML DOM Parser之類的庫。有了他,您可以在HTML頁面上使用jQuery之類的選擇器來查找標簽。
然后為前。 您可以使用類似的方法來獲取所有元素。
// 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:"People who visit this",onComplete:show.onPeopleOpening,onCleanup:show.onPeopleClosing,href:"/fatora/alrajhi/invoices/3687908533/people"}); return false">789621 paid</a> show this</div></code>';
preg_match_all("#(\d*)[ a-z]*?</a>#i",$input,$out);
echo $out[1][0];
?>
您可以將$input
的值替換為file_get_contents("URL HERE")
;
一旦有了頁面,就可以使用Javascript或jQuery對其進行剪貼,並通過定位類名來獲取所需的onclick值。
您將需要一個simple_html_dom來從頁面中刪除發票。 然后使用
$ret = $html->find('div[id]');
檢索包含發票的div。
$content=$ret->innertext;
給出該div的內容。 休息很簡單。 只需將$ content與''(空格)分開,並獲取$ invoice
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.