将ereg_replace转换为preg_replace [英] Converting an ereg_replace to preg_replace
问题描述
我必须将 ereg_replace
转换为 preg_replace
ereg_replace代码为:
The ereg_replace code is:
ereg_replace( '\$([0-9])', '$\1', $value );
preg由开始和结束反斜杠表示,我认为转换是:
As preg is denoted by a start and end backslash I assume the conversion is:
preg_replace( '\\$([0-9])\', '$\1', $value );
由于我对正则表达式没有很好的了解,所以我不确定以上是否是
As I don't have a good knowledge of regex I'm not sure if the above is the correct method to use?
推荐答案
ereg_replace()
和 preg_replace()
是该模式必须用定界符括起来:定界符+模式+定界符
。如文档所述,分隔符可以是任何非分隔符。 -字母数字,非反斜杠,非空格字符。这意味着有效的定界符为: /
,#
,〜
, +
,%
, @
,!
和<>
,其中前两个最常用(但这只是我的猜测)。
One of the differences between ereg_replace()
and preg_replace()
is that the pattern must be enclosed by delimiters: delimiter + pattern + delimiter
. As stated in the documentation, a delimiter can be any non-alphanumeric, non-backslash, non-whitespace character. This means that valid delimiters are: /
, #
, ~
, +
, %
, @
, !
and <>
, with the first two being most often used (but this is just my guess).
如果您的 ereg_replace()
可以按预期工作,则只需在模式中添加定界符即可。以下所有示例均适用:
If your ereg_replace()
worked as you expected, then simply add delimiters to the pattern and it will do the thing. All examples below will work:
preg_replace('/\$([0-9])/', '$\1', $value);
或
preg_replace('#\$([0-9])#', '$\1', $value);
或
preg_replace('%\$([0-9])%', '$\1', $value);
这篇关于将ereg_replace转换为preg_replace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!