我想从句子中找到单词的第一个字符,我想用其他字符替换它。
eg '9322731525, 9820231525, 23856141, 9321154466'
在第一个位置找到字符2并替换它,使输出看起来像
eg '9322731525, 9820231525, 93856141, 9321154466'。
我尝试使用substr
,但它没有工作。
您需要这样做:'/ 2/'
加空格和a,
替换第一个字母:
$string = '9322731525 , 9820231525, 23856141 , 9321154466';
$string = str_replace(',', ', ',$string);
$string = preg_replace('/ 2/', ' 9', $string);
output : 9322731525 , 9820231525, 93856141 , 9321154466
替换整个单词:
$string = '9322731525 , 9820231525, 23856141 , 9321154466';
$string = str_replace(',', ', ',$string);
$string = preg_replace('/ (2[0-9]* )/', ' yourNewWord ', $string);
output : 9322731525 , 9820231525, yourNewWord , 9321154466
不是世界上最好的正则表达式,但我认为它可以满足你的需要
$s='9322731525, 9820231525, 23856141, 9321154466';
$pttn='@'s2.@';
echo preg_replace( $pttn, ' 9', $s );
outputs
-------
9322731525, 9820231525, 9856141, 9321154466
你可以试试
substr_replace("2","9",0,1);
看一下这里,看看这个函数是如何工作的
use this:
echo preg_replace('/2/', '9', $yourstring);