'How to drop the first digit in phone number in php

I want to drop the first digit in phone number in php (for laravel framework). For example, if the user enter 05030123456, I want to get +2335030123456 into the database. That is, if my phone column in the database is phone_number, I will normally have the code in the controller look like this:

$user->phone_number = $request->phone

So, how can I treat $request->phone so it send +2335030123456 to the database for $user->phone_number instead of 05030123456.



Solution 1:[1]

If you want predefined '+233' then below code will be help you.

$user->phone_number = '+233'.substr($request->phone, 1);

So, your output will be : +2335030123456.

Solution 2:[2]

use string manipulation their is Str class in Laravel it's very useful

\Str::replaceFirst('0','+233', $request->phone)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ravikant Patel
Solution 2