用PHP字符串中的价格符号包裹字符串

时间:2015-02-06 16:56:45

标签: php

如何转动

$str = "Shoes cost $10, and pants cost $25obo";

$str = "Shoes cost <em>$10></em>, and pants cost <em>$25obo</em>";

使用php。我需要一种对许多文本都有效的方法,比如许多段落,而不仅仅是一个句子。

1 个答案:

答案 0 :(得分:1)

还有其他方法可以使用PHP,但您可以使用REGEX

尝试preg_replace()模式的这种方式

enter image description here

<强> CODE

$re = "/(\\$\\d+\\w*)/";
$str = "Shoes cost \$10, and pants cost \$25obere\nShoes cost \$10, and pants cost \$25obe \nShoes cost \$10, and pants cost \$25obfe\nShoes cost \$10, and pants cost \$25obee\n Shoes cost \$10, and pants cost \$25obo";
$subst = "<em>$1</em>";

$result = preg_replace($re, $subst, $str);
print $result;

<强>输出

Shoes cost <em>$10</em>, and pants cost <em>$25obo</em>
Shoes cost <em>$10</em>, and pants cost <em>$25TE</em> 
Shoes cost <em>$10</em>, and pants cost <em>$25ded</em>
Shoes cost <em>$10</em>, and pants cost <em>$25o</em>
Shoes cost <em>$10</em>, and pants cost <em>$25obo</em>

DEMO