2011-11-16 65 views
21

可能重複時, 「分隔符不能是字母,數字或反斜線」 錯誤:
Converting ereg expressions to preg故障診斷更改額日格()來的preg_match()

<?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()在C:\ P中不推薦使用rogram Files \ EasyPHP-5.3.8.1 \ www \ m \ img.php on line 5

不推薦使用:函數ereg()在C:\ Program Files \ EasyPHP-5.3.8.1 \ www \ m \ img中不推薦使用。上行PHP 6

的preg_match()函數得到這個誤差

警告:的preg_match()[function.preg匹配]:分隔一定不能在C字母數字或反斜槓:\ Program Files文件\第6行EasyPHP-5.3.8.1 \ www \ m \ img.php

警告:preg_match()[function.preg-match]:Del imiter不能在C字母數字或反斜槓:\ Program Files文件\的EasyPHP-5.3.8.1 \第7行

+0

鏈接的問題可以回答他的問題,但如何使這些問題重複? –

回答

45
  1. ereg已被棄用。不要使用它。
  2. preg函數都是「Perl正則表達式」,這意味着你需要在你的正則表達式上有某種開始和結束標記。通常這將是/#,但任何非字母數字都會很好。

例如,這些將工作:

preg_match("/foo/u",$needle,$haystack); 
preg_match("#foo#i",$needle,$haystack); 
preg_match("@[email protected]",$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! 
+0

感謝您的解釋! – gordon33

+1

當我嘗試preg_match(「/ foo/g」,$ needle,$ haystack);它不起作用。我有一個PHP警告,未知修飾符'g' – James

+0

根據另一個問題[此答案](http://stackoverflow.com/a/3578703/1676444),'/ g'模式修飾符在PHP中不起作用。您需要在您的示例中使用'preg_match_all'。 [PHP的模式修飾符列表](http://php.net/manual/en/reference.pcre.pattern.modifiers.php) – Turnerj

3

WWW \米\ img.php隨着preg_match()您正則表達式必須開始,並用分隔符結尾如/除了少數例外情況(例如最後加上「i」不區分大小寫)。

例如

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

「分隔符可以是任何非字母數字,非反斜槓,非空白字符。」 [手動](http://www.php.net/manual/en/regexp.reference.delimiters.php)。但是,是的,你是對的,分隔符不見了。 – lonesomeday

+0

啊是的。謝謝,我會更新我的文章! –