2015-06-20 84 views
1

這裏是我的代碼:的preg_match將不會返回該行,但檢測到它(PHP)

  $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, "http://comic.naver.com/webtoon/detail.nhn?titleId=570506&no=99&weekday=thu"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1"); 
     $result = curl_exec($ch); 
     curl_close($ch); 

$matches = array(); 
     preg_match('/^.*\bcontent_image_0\b.*$/um', $result, $matches, PREG_OFFSET_CAPTURE); 

var_dump($matches); 

我添加/ UM到底是因爲網站上的字符集是UTF-8。

我想要做的是找到包含「content_image_0」的行並返回整行。

這是什麼var_dump($匹配);轉儲:

array(1) { [0]=> array(2) { [0]=> string(383) " " [1]=> int(25958) } } 

我們可以看到,它檢測383串,但不返回他們的報價是空的:/

+1

模式是好的,但你看不到結果,因爲HTML標籤沒有出現在您的瀏覽器。所以右鍵單擊並顯示頁面源代碼。或者使用'echo htmlspecialchars($ matches [0] [0]);'在瀏覽器中顯示字符串或'echo htmlspecialchars(print_r($ matches,true));' –

+0

啊,謝謝你的回答。 – TeachMeHypertext

回答

0

要捕獲的比賽(這比在整場比賽不同的),你需要使用捕獲組,由parens ()指示,圍繞您要捕獲的數據。例如,爲了捕捉整個行:

preg_match('/^(.*\bcontent_image_0\b.*)$/um', $result, $matches, PREG_OFFSET_CAPTURE); 

捕獲並返回所有383匹配的行/線,你需要使用preg_match_all()每行,你可以做一個捕獲組如下:

preg_match_all('/^(.*\bcontent_image_0\b.*)$/um', $result, $matches, PREG_OFFSET_CAPTURE); 
0

您必須使用捕獲組是這樣的:

preg_match('/^(.*\bcontent_image_0\b.*)$/um', $result, $matches, PREG_OFFSET_CAPTURE); 
       ^---  here  ---^ 
相關問題