Jump to content

PHP Split Telephone Number


Danny620

Recommended Posts

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'

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.