Replace ‘0’ with 1 in phone numbers Laravel

Tags: , , ,

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']);

Source: stackoverflow