我有一个这样的输入: $input="12050301000000000000";
替换所有的字符串,除了两个指标
我试图用preg_replace
改变我输入0
每一件事情,除了通过它们的索引引用的2个字符的。
例如我想,除了第一和第二字符的替换一切为0
我尝试这样做:
$input="02050301000000000000";
$index1=0;
$index2=1;
$output= preg_replace('/(?!^'.$index1.')/', '0', $input);
为什么不是一个简单的:'$输出= str_repeat( '0',strlen的($输入)); $ output [$ index1] = $ input [$ index1]; $ output [$ index2] = $ input [$ index2];',这会比正则表达式更容易阅读和理解 –
在评论中无法获得比这更好的答案! –
@MarkBaker我喜欢你的答案,你可以发布它作为答案,以便我可以接受它。 –