Troubleshooting “Delimiter must not be alphanumeric or backslash” error when changing ereg() to preg_match() [duplicate]

不羁岁月 提交于 2019-11-28 09:01:13
  1. ereg is deprecated. Don't use it.
  2. The preg functions 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.

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!

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).

e.g.

preg_match('/[regex]/i', $string)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!