在PHP中查找并替换除第一个数字以外的数字
我想替换字符串中的某个数字,但不更改php中的第一个数字 例如,将所有4更改为5:在PHP中查找并替换除第一个数字以外的数字,php,Php,我想替换字符串中的某个数字,但不更改php中的第一个数字 例如,将所有4更改为5: 34 => 35 148 => 158 2449 => 2559 15540 => 15550 43 => 43 (Don't change start 4) 450 => 450 (Don't change start 4) 4540 => 4550 (only change second 4) 必须是字符串-如果需要,请先转换为字符串,然后再转换回int。尝试以下
34 => 35
148 => 158
2449 => 2559
15540 => 15550
43 => 43 (Don't change start 4)
450 => 450 (Don't change start 4)
4540 => 4550 (only change second 4)
必须是字符串-如果需要,请先转换为字符串,然后再转换回int。尝试以下操作:
<?php
$a="4540";//As described in question its an string but it can even be a no auto type conversion will do the rest
$tem=$a[0];
$a=str_ireplace('4', '5', $a);
$a[0]=$tem;
echo $a;//or do what ever you want with $a
<?php
$no="445486";
for($i=1;$i<strlen($no);$i++){
if($no[$i]=="4")
$no[$i]="5";
}
echo $no;
保存第一个字符,替换剩余部分中的4->5,并与保存的第一个字符连接
<?php
$no="445486";
for($i=1;$i<strlen($no);$i++){
if($no[$i]=="4")
$no[$i]="5";
}
echo $no;