将ereg()更改为preg_match()时,疑难解答"分隔符不能是字母数字或反斜杠"错误

gor*_*n33 21 php regex preg-match ereg

可能重复:
将ereg表达式转换为preg

<?php
$searchtag = "google";
$link = "http://images.google.com/images?hl=de&q=$searchtag&btnG=Bilder-Suche&gbv=1";
$code = file_get_contents($link,'r');
ereg("imgurl=http://www.[A-Za-z0-9-]*.[A-Za-z]*[^.]*.[A-Za-z]*", $code, $img);
ereg("http://(.*)", $img[0], $img_pic);
echo '<img src="'.$img_pic[0].'" width="70" height="70">'; ?> 
Run Code Online (Sandbox Code Playgroud)

我得到这个错误

不推荐使用:函数ereg()在第5行的C:\ Program Files\EasyPHP-5.3.8.1\www\m\img.php中已弃用

不推荐使用:函数ereg()在第6行的C:\ Program Files\EasyPHP-5.3.8.1\www\m\img.php中已弃用

preg_match()函数给出了这个错误

警告:preg_match()[function.preg-match]:在第6行的C:\ Program Files\EasyPHP-5.3.8.1\www\m\img.php中,分隔符不能是字母数字或反斜杠

警告:preg_match()[function.preg-match]:在第7行的C:\ Program Files\EasyPHP-5.3.8.1\www\m\img.php中,分隔符不能是字母数字或反斜杠

cwa*_*ole 45

  1. ereg已弃用.不要使用它.
  2. 这些preg函数都是"Perl正则表达式",这意味着您需要在正则表达式上使用某种开头和结尾标记.通常这将是/#,但任何非字母数字都可以.

例如,这些将起作用:

preg_match("/foo/u",$needle,$haystack);
preg_match("#foo#i",$needle,$haystack);
preg_match("@foo@",$needle,$haystack);
preg_match("\$foo\$w",$needle,$haystack); // bad idea because `$` means something
                                          // in regex but it is valid anyway
                                          // also, they need to be escaped since
                                          // I'm using " instead of '
Run Code Online (Sandbox Code Playgroud)

但这不会:

preg_match("foo",$needle,$haystack); // no delimiter!
Run Code Online (Sandbox Code Playgroud)