$numberPhone = '4155551212'; $firstChain = substr($numberPhone, 0, 3); $secondChain = substr($numberPhone, 3, 3); $thirdChain = substr($numberPhone, 6, 4); $formatedNumberPhone = '(' . $firstChain . ') ' . $secondChain . '-' . $thirdChain; echo $formatedNumberPhone;
Here is a solution for those who have a similar question.
It explains a little how substr () works:
In this case, three arguments:
- The chain you want to change
- The index that represents the place where the function starts its process
- How many characters do you want to keep
Please note that you can pass a negative value to the second and third argument (refer to the white paper for more information).
In this case, I take the first character of the phone number, so I will say that the function starts with 0 and hold 3 characters, so it looks like this: susbtr($numberPhone, 0, 3) .
Hope this helps!
source share