24

可能重复:
将 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\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 中的分隔符不能是字母数字或反斜杠

4

2 回答 2

47
  1. ereg已弃用。不要使用它。
  2. 这些preg函数都是“Perl 正则表达式”,这意味着你需要在你的正则表达式上有某种开始和结束标记。通常这将是/or #,但任何非字母数字都可以。

例如,这些将起作用:

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!
于 2011-11-16T22:29:02.657 回答
3

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

例如

preg_match('/[regex]/i', $string)
于 2011-11-16T22:27:48.943 回答