【发布时间】:2018-03-16 23:24:25
【问题描述】:
我在 PCRE 方法中有现有的代码,我希望在 POSIX 中具有相同的功能。 下面是我在 PCRE 中做的示例代码。
<?php
$regex = "/(\d)/";
$content = "In the garden have dog,cat,23242,rabbit.";
echo preg_replace($regex,"<span style='color:green'>$1</span>",$content);
//Result:
//In the garden have dog,cat,<span style='color:green'>2</span><span style='color:green'>3</span><span style='color:green'>2</span><span style='color:green'>4</span><span style='color:green'>2</span>,rabbit.
我正在尝试在 POXIS 中做同样的事情,但无法获得相同的输出。 下面是我在 POSIX 中做的示例代码。;
<?php
$regex = "([[:digit:]])";
$content = "In the garden have dog,cat,23242,rabbit."
echo ereg_replace($regex,"<span style='color:green'>$1</span>",$content);
//Result:
//In the garden have dog,cat,<span style='color:green'>$1</span><span style='color:green'>$1</span><span style='color:green'>$1</span><span style='color:green'>$1</span><span style='color:green'>$1</span>,rabbit.
【问题讨论】:
-
如果
preg_*可以满足您的所有需求,为什么还需要ereg?好吧,要使ereg函数工作相同,只需将替换字符串中的$1替换为\\1。 -
我使用的集成系统是只读的posix。感谢您的回答,正在工作。
-
POSIX 模式(所有 ereg_* 函数)自 php 5.3 起已弃用。请不要使用它们。它们已从 php 7.0 中完全删除
-
您可以将posix 与
preg_replace一起使用,您也可以使用量词并一次替换所有数字,还是一次替换一个? 3v4l.org/l069J 没有量词你会得到多个跨度。
标签: php regex posix-ere ereg-replace