2016-03-15 28 views
0

在我的代碼中,當我將html內容分配給變量但不與url path一起使用時,我的Regexp正常工作。我得到空陣列。正則表達式不能使用cURL url內容

<?php 
    $productmfgno = "154637401"; 
       $url = "http://www.pandorasoem.com/search#q=".$productmfgno; 
       $ch1= curl_init(); 
       curl_setopt ($ch1, CURLOPT_URL, $url); 
       curl_setopt($ch1, CURLOPT_HEADER, 0); 
       curl_setopt($ch1,CURLOPT_VERBOSE,1); 
       curl_setopt($ch1, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)'); 
       curl_setopt ($ch1, CURLOPT_REFERER,'http://www.google.com'); //just a fake referer 
       curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); 
       curl_setopt($ch1,CURLOPT_POST,0); 
       curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 20); 
       $htmlContent= curl_exec($ch1); 
       curl_close($ch1); 
     /* It works when I assign this html content to $htmlContent variable but not working with cURL url 

    $htmlContent = '<div class="findify-navigation-header findify-clearfix"> <div class="findify-pagination findify-push-right"></div> <div class="findify-header">Showing 2 results for <span class="findify-query">"154637401"</span>. <span id="findify-didyoumean"></span></div> </div>'; 

*/ 
       preg_match_all('/<div.*class=\"findify\-header\".*?>(.*?)<span.*class=\"findify-query\">.*?<\/div>/Us', $htmlContent, $count); 
       print_r($count); 

預期結果 - Showing 2 results for

這樣我就可以獲取該結果計數。

+0

post sample respone – Vishnu

+0

我得到空數組。 – Nikita

+0

http://www.pandorasoem.com/search#q=154637401 - 此頁面上的源碼不包含您提到的任何html,它正在通過javascript加載結果 – Vishnu

回答

1
+0

我正要發佈它 – Vishnu

+0

所以我需要應用'正則表達式'到這個URL ?我想從搜索結果中獲取產品數量。所以我可以將數據提取到每個產品。 – Nikita

+0

是的,你需要應用一個正則表達式到這個URL。但是你需要重寫它(參見** UPD **)。 – s1lent1um