Page 1 of 1

[SOLVED]String Manipulation

Posted: Wed Mar 02, 2005 8:55 am
by alix
i've been going through the string functions Here And yet i couldnt find anything to really help me, (as usual) what im trying to do is take a phone number, like 555-555-5555 and remove the hyphens. or just basicly make it a 10 digit number with no spaces, hyphens, or periods.. when my script pulls the data from the database it needs to fix the phone numbers before it creates the .csv.

This is what i have.

Code: Select all

<?
header ("Content-type: text/csv"); 


$query = "SELECT * FROM cb "; 

$result = mysql_query($query) or die(mysql_error()); 
    echo "id, name, phone, address\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) &#123; 
   echo trim($line&#1111;'unique']).",".trim($line&#1111;'name']).",".trim($line&#1111;'phone']).",".trim($line&#1111;'address'])."\n"; 
&#125; 

?>
Thanks,

Posted: Wed Mar 02, 2005 9:02 am
by Jean-Yves
I think that you can use eregi_replace() for that purpose.

Posted: Wed Mar 02, 2005 9:31 am
by alix
Thanks...

Not sure how i would make the pattern for Spaces, periods, and commas though.

Anyone have a link where i can learn how to write the patterns?

Code: Select all

<?
$string = "555-555-9874 454.75";

$var = eregi_replace("-", "", $string); 

echo $var;
?>
That just replaces the hyphens

Posted: Wed Mar 02, 2005 9:52 am
by feyd
ereg* slow, preg* faster.. str_replace() faster still.

Posted: Wed Mar 02, 2005 10:16 am
by alix
awesome feyd... str_replace() was much easier. It got the job done... thanks again.