JSHINER Posted April 2, 2007 Share Posted April 2, 2007 I currently have: str_replace('-', '', trim($row[5])); Which makes a phone number of 555-555-5555 display as 5555555555. How can I also have it replace . so 555.555.555 will display as 5555555555 also. Thanks Quote Link to comment Share on other sites More sharing options...
only one Posted April 2, 2007 Share Posted April 2, 2007 here $display=555-555-5555; $bbcode = array('.', '-'); $html = array( '', '' ); $display = preg_replace($bbcode, $html, $display); now $display will echo 5555555555 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 2, 2007 Share Posted April 2, 2007 Just replace the "-" with a "." in your str_replace() function. If you want to replace any occurrence of "-" or "." in the string use an array: <?php $str = array(); $str[] = '555-555-5555'; $str[] = '555.555.5555'; $str[] = '555.555-5555'; for ($i=0;$i<count($str);$i++) echo 'Before: ' . $str[$i]. ' ===> After: ' . str_replace(array('-','.'),'',$str[$i]) . '<br>'; ?> Ken Quote Link to comment Share on other sites More sharing options...
JSHINER Posted April 2, 2007 Author Share Posted April 2, 2007 I am using the code to import from a .csv to a sql database. So I need it to take the data from $row[5] and manipulate that... and create a final variable to import... example now I have: $phone = str_replace('-', '', trim($row[4])); I need it to replace - in a phone number or . in a phone number - however they are formatted. From what I uderstand - will this work? $phone = str_replace(('-','.'), '', trim($row[4])); Quote Link to comment Share on other sites More sharing options...
Lumio Posted April 2, 2007 Share Posted April 2, 2007 Try it I think for you it's the best way to use it like that: <?php $phone = '555.555-555.555-55'; $phone = str_replace('-', '', $phone); $phone = str_replace('.', '', $phone); echo $phone; ?> Quote Link to comment Share on other sites More sharing options...
per1os Posted April 2, 2007 Share Posted April 2, 2007 I am not sure if str_replace has this functionality, but ereg_replace works too. Most people shy away from it because it takes "longer" to process, but really unless you are running this 5,000 times you should not see a difference. <?php $phone = '555.555-555.555-55'; $phone = ereg_replace('-|.', '', $phone); echo $phone; ?> Now I cannot remember if the "." is a special character if so do this: $phone = ereg_replace('-|\.', '', $phone); That should work how you want it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.