2
這裏可能有一個正則表達式錯誤,但我無法解決它。preg_match_all undefined偏移錯誤
我得到「Undefined offset: 0
」錯誤,無法獲得標題的值。
這是我的代碼。 INEEDTHISONE是我想要取得的頭銜。
for ($j = 1; $j <= 1; $j++) {
for ($i = 0; $i < 5; $i++) {
$site = file_get_contents("http://example.com/index.php?route=product/category&path=395&page=$j");
preg_match_all('@<div class="name csstwo" title="INEEDTHISONE"><a itemprop="name" href="http://example.com/id/(.*?)/(.*?).html&path=(.*?)">(.*?)</a></div>@', $site, $title);
$title[2][$i] = strip_tags($title[2][$i]); // strip tags
$title[2][$i] = preg_replace('~.*?>~', '', $title[2][$i]); // remove a tag and messy stuff
echo $title[2][$i]."<br>"; // gives undefined offset error.
}
詳細錯誤。
line24 = $title[2][$i] = strip_tags($title[2][$i]);
line27 = echo $title[2][$i]."<br>";
Notice: Undefined offset: 0 in /Applications/MAMP/htdocs/test3.php on line 24
Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/test3.php on line 27
Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/test3.php on line 24
Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/test3.php on line 24
Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/test3.php on line 27
Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/test3.php on line 24
Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/test3.php on line 27
Notice: Undefined offset: 4 in /Applications/MAMP/htdocs/test3.php on line 24
Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/test3.php on line 27
返回空白頁面,isset()返回false。 – salep 2015-02-10 09:29:18
是的,這只是沒有錯誤的。你可能會面對其他問題,比如匹配。爲此給出您的輸入樣本和輸出樣本。這將從'file_get_contents()' – 2015-02-10 09:32:37
得到我通過改變preg_match_all解決了這個問題,但是謝謝你的回答。 – salep 2015-02-10 10:03:39