1

我知道有很多关于同样问题的问题。我阅读了它们,但我无法修复我的代码。

<?
$tabla = "<table>
<tr>
<td>
<a class='texto'>$ 2,123.01</a>
</td>
<td>
asddasdsad$,.$$$
</td>
</tr>
</table>";
echo preg_replace("<a class='texto'>\$ ([0-9]*),([0-9]*).([0-9]*)</a>", "<a class='texto'>$0$1,$2</a>", $tabla);

?>

PHP 错误:警告:preg_replace() [function.preg-replace]: Unknown modifier '$'

我想得到:

<?
<table>
<tr>
<td>
<a class='texto'>2123,01</a>
</td>
<td>
asddasdsad$,.$$$
</td>
</tr>
</table>
?>

我在这里http://regexpal.com/尝试并测试了我的正则表达式并工作。但是我在 preg_replace 中有一些问题。

4

2 回答 2

3

你犯了三个错误:

  1. 双引号内的 \$ 仅表示 $ ,正则表达式将其视为与行尾匹配
  2. 您忘记了模式分隔符
  3. $0 指的是整个字符串。括号中的表达式称为 $1、$2 等。

echo preg_replace("|<a class='texto'>\\\$ ([0-9]*),([0-9]*).([0-9]*)</a>|", "<a class='texto'>$1$2,$3</a>", $tabla);
于 2013-09-24T02:51:10.307 回答
1

正则表达式需要围绕实际表达式的分隔符

<a class='texto'>\$ ([0-9]*),([0-9]*).([0-9]*)</a>

需要像:

/<a class='texto'>\$ ([0-9]*),([0-9]*).([0-9]*)<\/a>/

并转义/表达式中可能包含的任何其他内容

或使用您的表达式中未出现的不同分隔符

#<a class='texto'>\$ ([0-9]*),([0-9]*).([0-9]*)</a>#

List of acceptable delimiters

于 2013-09-24T02:49:22.840 回答