故障排除”;分隔符不能是字母数字或反斜杠“;将ereg()更改为preg_match()时出错


Troubleshooting "Delimiter must not be alphanumeric or backslash" error when changing ereg() to preg_match()

可能重复:
将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">'; ?> 

我得到这个错误

不推荐使用:函数ereg((在第5行上的C:''Program Files''EasyHP-5.3.81''www''m''img.php中不推荐使用

不推荐使用:函数ereg((在第6行上的C:''Program Files''EasyHP-5.3.81''www''m''img.php中不推荐使用

preg_match((函数为该错误提供

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

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

  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 '

但这不会:

preg_match("foo",$needle,$haystack); // no delimiter!

对于preg_match(),正则表达式必须以分隔符(如/(开头和结尾,但很少有例外(例如在末尾添加"i"以区分大小写(。

例如

preg_match('/[regex]/i', $string)