php 将 ereg() 更改为 preg_match() 时对“分隔符不得为字母数字或反斜杠”错误进行故障排除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8159628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:04:39  来源:igfitidea点击:

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

phpregexpreg-matchereg

提问by gordon33

Possible Duplicate:
Converting ereg expressions to preg

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

And i get this error

我得到这个错误

Deprecated: Function ereg() is deprecated in C:\Program Files\EasyPHP-5.3.8.1\www\m\img.php on line 5

Deprecated: Function ereg() is deprecated in C:\Program Files\EasyPHP-5.3.8.1\www\m\img.php on line 6

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

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

preg_match() functions give this error

preg_match() 函数给出这个错误

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\Program Files\EasyPHP-5.3.8.1\www\m\img.php on line 6

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\Program Files\EasyPHP-5.3.8.1\www\m\img.php on line 7

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

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

回答by cwallenpoole

  1. eregis deprecated. Don't use it.
  2. The pregfunctions are all "Perl regular expressions" meaning you need to have some sort of beginning and end marker on your regex. Often this will be /or #, but any non alpha-numeric will do fine.
  1. ereg已弃用。不要使用它。
  2. 这些preg函数都是“Perl 正则表达式”,这意味着您需要在正则表达式上有某种开始和结束标记。通常这将是/or #,但任何非字母数字都可以。

For example, these will work:

例如,这些将起作用:

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 '

But this will not:

但这不会:

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

回答by Yes Barry

With preg_match()your regex must begin and end with a delimiter such as /with few exceptions (for example adding "i" at the end for case-insensative).

随着preg_match()你的正则表达式必须开始和结束分隔符,例如/除了少数例外(例如添加“我”末的情况下,insensative)。

e.g.

例如

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