简体   繁体   中英

Replace '0' with 1 in phone numbers Laravel

I'm trying to replace users phone numbers to start with 1 instead of 0 but I'm stuck.

I have this line $mobileNumber = implode(',', $postData['phone']);

which outputs "0445329500,0569075729,0456786942" I want to replace all the first 0 with 1 so instead of 0445329500,0569075729,0456786942 it should be 1445329500 , 1569075729 , 1456786942

I have tried this Str::replaceFirst('0','1',$mobileNumber); it only replaces the first number 1445329500

Any help will be appreciated.

rather than implode, I suggest you loop through the array and update the numbers there.

foreach ($postData['phone'] as $index => $value) {
    $postData['phone'][$index] = Str::replaceFirst('0','1',$value);
}
//then you can implode
$mobileNumber = implode(',', $postData['phone']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM