drcvvkx914772
drcvvkx914772
2014-03-10 18:50

需要来自url的特定数据和file_get_contents()

已采纳

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>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答

  • dq1685513999 dq1685513999 7年前

    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

    点赞 评论 复制链接分享
  • duanba8173 duanba8173 7年前

    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

    点赞 评论 复制链接分享
  • duanaoyuan7202 duanaoyuan7202 7年前

    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.

    点赞 评论 复制链接分享
  • dtyz76562 dtyz76562 7年前
    <?
    
    $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");

    点赞 评论 复制链接分享
  • dongyingdao8867 dongyingdao8867 7年前

    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>';
    
    点赞 评论 复制链接分享

相关推荐