Jump to content

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.

geavns: mysql_query only support single queries

 

mystifier:

Something like

update contacts set phone =  replace(replace(replace(replace(phone, ')', ''), '(', ''), '-', ''), ' ', '')

would probably work... though it's not pretty...

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.