Jump to content

[SOLVED] Strip non-numeric characters


mystifier

Recommended Posts

The following code is used to strip non-numeric characters from a very large table:

 

$prep1 = @mysql_query("update contacts set phone =  replace(phone, ' ', '')");

$prep2 = @mysql_query("update contacts set phone =  replace(phone, '-', '')");

$prep3 = @mysql_query("update contacts set phone =  replace(phone, '(', '')");

$prep4 = @mysql_query("update contacts set phone =  replace(phone, '), '')");

 

Can anyone supply a single-pass solution?

 

Link to comment
https://forums.phpfreaks.com/topic/157796-solved-strip-non-numeric-characters/
Share on other sites

My only thought is to put it all into one line (but still run four queries as replace() will only accept a string, not an array of variables to replace;

 

$prep1 = @mysql_query("update contacts set phone =  replace(phone, ' ', '');update contacts set phone =  replace(phone, '-', '');update contacts set phone =  replace(phone, '(', '');update contacts set phone =  replace(phone, '), '')");

If this data is already in the database, then it doesn't matter so much. The script to correct it only needs to be run once, even if it's slow or needs multiple passes.

 

From then on only insert the data if it's in the correct format. Do any string modification in PHP beforehand - and there are more efficient ways to do it in PHP than in SQL.

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.