mystifier Posted May 12, 2009 Share Posted May 12, 2009 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 More sharing options...
gevans Posted May 12, 2009 Share Posted May 12, 2009 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, '), '')"); Link to comment https://forums.phpfreaks.com/topic/157796-solved-strip-non-numeric-characters/#findComment-832238 Share on other sites More sharing options...
Carth Posted May 12, 2009 Share Posted May 12, 2009 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. Link to comment https://forums.phpfreaks.com/topic/157796-solved-strip-non-numeric-characters/#findComment-832272 Share on other sites More sharing options...
Mchl Posted May 12, 2009 Share Posted May 12, 2009 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... Link to comment https://forums.phpfreaks.com/topic/157796-solved-strip-non-numeric-characters/#findComment-832277 Share on other sites More sharing options...
Adam Posted May 12, 2009 Share Posted May 12, 2009 $prep1 = mysql_query(" update contacts set phone = replace(phone, ' ', ''), phone = replace(phone, '-', ''), phone = replace(phone, '(', ''), phone = replace(phone, ')', '') "); Link to comment https://forums.phpfreaks.com/topic/157796-solved-strip-non-numeric-characters/#findComment-832280 Share on other sites More sharing options...
Mchl Posted May 12, 2009 Share Posted May 12, 2009 Oooo... nice Link to comment https://forums.phpfreaks.com/topic/157796-solved-strip-non-numeric-characters/#findComment-832289 Share on other sites More sharing options...
fenway Posted May 12, 2009 Share Posted May 12, 2009 FYI, that's only guaranteed to work for single-table update statements. Link to comment https://forums.phpfreaks.com/topic/157796-solved-strip-non-numeric-characters/#findComment-832613 Share on other sites More sharing options...
mystifier Posted May 13, 2009 Author Share Posted May 13, 2009 Thanks Guys, I didn't think about trying to nest replaces which is nice but I think the mutiple set is the clearer way to go with my weaker than average brain. Link to comment https://forums.phpfreaks.com/topic/157796-solved-strip-non-numeric-characters/#findComment-833162 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.