Jump to content

str_replace Question


JSHINER

Recommended Posts

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

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

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

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.