I want to edit string like below example :
$str = '$variable$123'
Convert to
$str = '$variable$*123'
I tried below code :
<?php
$str = '$variable$123';
$str= preg_replace('/(\$\w*\$.[0-9]*)+/i', '$1*$2', $str);
echo $str;
?>
But didn't get as i want...
Please help.....
I got the answer.
<?
$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
}
?>
output :
$variable$*123*$variable2$
$variable$*123
$str = preg_replace('/(\$\w+\$)(\d+)/', '$1*$2', $str);
Another (simpler) version that may fit the exemple given in comment
$str = preg_replace('/(\$\w+\$)/', '$1*', $str);