我想编辑如下示例所示的字符串:
$str = '$variable$123'
转换为
$str = '$variable$*123'
我尝试了以下代码:
<?php
$str = '$variable$123';
$str= preg_replace('/('$'w*'$.[0-9]*)+/i', '$1*$2', $str);
echo $str;
?>
但没有得到我想要的。。。
请帮忙。。。。。
$str = preg_replace('/('$'w+'$)('d+)/', '$1*$2', $str);
另一个(更简单的)版本,可能符合评论中给出的示例
$str = preg_replace('/('$'w+'$)/', '$1*', $str);
<?
$str1 = '$variable$123$variable2$';
echo replace($str1);
$str2 = '$variable$123';
echo replace($str2);
function replace($str)
{
$str = preg_replace('/('$'w*'$)('d+)/i', '$1*$2', $str);
$str = preg_replace('/('d+)('$'w*'$)/i', '$1*$2', $str);
return $str
}
?>
输出:
$variable$*123*$variable2$
$variable$*123