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 Link to comment https://forums.phpfreaks.com/topic/45323-str_replace-question/ 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 Link to comment https://forums.phpfreaks.com/topic/45323-str_replace-question/#findComment-220060 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 Link to comment https://forums.phpfreaks.com/topic/45323-str_replace-question/#findComment-220077 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])); Link to comment https://forums.phpfreaks.com/topic/45323-str_replace-question/#findComment-220091 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; ?> Link to comment https://forums.phpfreaks.com/topic/45323-str_replace-question/#findComment-220115 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. Link to comment https://forums.phpfreaks.com/topic/45323-str_replace-question/#findComment-220138 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.