Home >Backend Development >PHP Tutorial > 求PHP一个正则轮换
求PHP一个正则替换
要把字符:cjy/dongnanya/flb-mnl/xianlu 替换为cjy/dongnanya-flb-mnl-xianlu
也就是讲把原来的字符除第一个“/”不替换,其它的都替换为“-”
$s = 'cjy/dongnanya/flb-mnl/xianlu';<br /> <br /> //方法一<br /> $t = preg_replace('#/#', '-', $s);<br /> $t = preg_replace('#-#', '/', $t, 1);<br /> echo $t;<br /> <br /> //方法二<br /> $n = 0;<br /> echo preg_replace_callback('#/#', 'foo', $s);<br /> <br /> function foo($r) {<br /> global $n;<br /> if($n++) return '-';<br /> return $r[0];<br /> }<br /> <br />