Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Splitting a telephone number in php with substr

David Carr

Tutorials PHP & MySQL

I recently had a collection of numbers in the format of area code then the telephone number with no spaces such as 01482123456 I needed a space after the area code so it would read 01482 123456 this is easily achieved using PHP's substr function.

$tel = '01482123456';
echo substr($tel, 0, 5).' '.substr($tel, 5, 6);

What the above is doing is printing the number to the page the first substr will show the area code, pass the function the number then specify where to start and how many digits to go in this case 5 then add a space then using another substr this time start at 5 digits in then and get the remainder of the number.<

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.