[SOLVED]String Manipulation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

[SOLVED]String Manipulation

Post 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,
Last edited by alix on Wed Mar 02, 2005 10:46 am, edited 1 time in total.
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post by Jean-Yves »

I think that you can use eregi_replace() for that purpose.
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ereg* slow, preg* faster.. str_replace() faster still.
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Post by alix »

awesome feyd... str_replace() was much easier. It got the job done... thanks again.
Post Reply