preg_match_all 未定义的偏移错误
preg_match_all undefined offset error
这里可能存在正则表达式错误,但我无法解决。
我收到“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.
}
详细错误。
第 24 行 = $title[2][$i] = strip_tags($title[2][$i]);
第 27 行 = 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
尝试更改以下内容:
for ($j = 1; $j <= 1; $j++) {
for ($i = 0; $i < 5; $i++) {
$site = file_get_contents("http://example.com.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);
if(isset($title[2][$i])){
$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.
}
}
我变了
preg_match_all('@<div class="name csstwo" title="INEEDTHISONE"><a itemprop="name" href="http://example.com/id/(.*?)/(.*?).html&path=(.*?)">(.*?)</a></div>@', $site, $title);
至
preg_match_all('@<div class="name csstwo" title="INEEDTHISONE"><a itemprop="name" href="http://example.com/id/(.*?)/(.*?).html&path=(.*?)">(.*?)</a></div>@', $site, $title);
问题解决了。
&
导致了这个问题。
这里可能存在正则表达式错误,但我无法解决。
我收到“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.
}
详细错误。
第 24 行 = $title[2][$i] = strip_tags($title[2][$i]);
第 27 行 = 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
尝试更改以下内容:
for ($j = 1; $j <= 1; $j++) {
for ($i = 0; $i < 5; $i++) {
$site = file_get_contents("http://example.com.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);
if(isset($title[2][$i])){
$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.
}
}
我变了
preg_match_all('@<div class="name csstwo" title="INEEDTHISONE"><a itemprop="name" href="http://example.com/id/(.*?)/(.*?).html&path=(.*?)">(.*?)</a></div>@', $site, $title);
至
preg_match_all('@<div class="name csstwo" title="INEEDTHISONE"><a itemprop="name" href="http://example.com/id/(.*?)/(.*?).html&path=(.*?)">(.*?)</a></div>@', $site, $title);
问题解决了。
&
导致了这个问题。