如何使用 "POSIX" 正则表达式替换正则表达式中的文本

How to replace text in regex by using "POSIX" regex

我有现有代码在 PCRE 方法中执行,我想在 POSIX 中具有相同的功能。 下面是我在 PCRE 中做的示例代码。

<?php

$regex = "/(\d)/";
$content = "In the garden have dog,cat,23242,rabbit.";

echo preg_replace($regex,"<span style='color:green'></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'></span>",$content);
//Result:
//In the garden have dog,cat,<span style='color:green'></span><span style='color:green'></span><span style='color:green'></span><span style='color:green'></span><span style='color:green'></span>,rabbit.

请注意 ereg_replace,

This function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0.

如果您的 PHP 环境不支持 preg_replace,请在替换模式中使用带有 \1 而不是 </code> 的当前代码。</p> <pre><code>$regex = "([[:digit:]]+)"; $content = "In the garden have dog,cat,23242,rabbit."; echo ereg_replace($regex,"<span style='color:green'>\1</span>",$content); // => In the garden have dog,cat,<span style='color:green'>23242</span>,rabbit.