Danny620 Posted January 11, 2011 Share Posted January 11, 2011 Hi, I have a number (int) stored in the database in this case a uk Telephone Number, its stored as like 01616276576 what i want to be able to do is pull it out of db and split it into chucks like 0161 627 6576 how can i do this? Link to comment https://forums.phpfreaks.com/topic/224126-php-split-telephone-number/ Share on other sites More sharing options...
Pikachu2000 Posted January 11, 2011 Share Posted January 11, 2011 Personally, I'd do it right in the query string, and alias the result. SELECT CONCAT_WS( ' ', SUBSTR(`tel_number`, 1, 4), SUBSTR(`tel_number`, 5, 3), SUBSTR(`tel_number`, AS `f_tel` FROM `table` WHERE `some_field` ='some_identifier' Link to comment https://forums.phpfreaks.com/topic/224126-php-split-telephone-number/#findComment-1158099 Share on other sites More sharing options...
Psycho Posted January 11, 2011 Share Posted January 11, 2011 You could also do it just as easily within the PHP code (not sure which would be more efficient on the server, but you could test it). (Assumes $row['phone'] is the db value) $formattedPhone = substr($row['phone'],0,4).substr($row['phone'],4,3).substr($row['phone'],7); You could even do it with a preg_replace() $formattedPhone = preg_replace("/(.{3})(.{4})(.{3})/", "\\1 \\2 \\3", $row['phone']); Link to comment https://forums.phpfreaks.com/topic/224126-php-split-telephone-number/#findComment-1158111 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.