Preg_replace,字符串和数字替换


preg_replace, string and numeric replacement

我猜这是preg_replace相关的问题,

我有一个具有多个重复模式的字符串

它们都被格式化为:

some string :22: more text :12: etc

我如何用一些不同的字符替换它们周围的":" ?

你可以这样做:

$string = 'some string :22: more text :12: etc';
$regex = '/:('d+):/';
$newString = preg_replace($regex, "@$1@", $string);

注意:您必须将第二个参数中的'@'替换为您想要的字符(数字前后的字符也不同)。

sbustudes _ for : around numbers:

preg_replace('/:('d+):/', '_$1_', 'some string :22: more text :12: etc');

编辑:误解原始问题。但是,仍然是一个灵活的选择:

$result = str_replace(":22:", "tag", "some string :22: more text :12: etc");
$result = str_replace(":12:", "other_tag", $result);

替换?