Forum Moderators: coopster

Message Too Old, No Replies

Replacing spaces with a character

         

jspeed

5:06 pm on Jan 10, 2008 (gmt 0)

10+ Year Member



I am pretty new to PHP & MySQL. I am trying to pull information out of a database, and replace the spaces with a character.

e.g. 2500 Vista Ridge Dr
with 2500+Vista+Ridge+Dr

I have read and tried different things, but have been unsuccessful getting the code right. If $address is my variable I want to replace the spaces in, wouldn't it be something like this?


str_replace(' ','+',$address);

Then in my loop I would call it like so:

$address=$row["address"];

Thanks in advance for any help.

cameraman

5:36 pm on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've got it, you just need to put the result somewhere, for example:
$address = str_replace(' ','+',$address);

jspeed

5:49 pm on Jan 10, 2008 (gmt 0)

10+ Year Member



Well... I put the code you suggested in, I must be doing something wrong. When I call it, it is still being displayed with the spaces. Am I suppose to put the str_replace when I define the variable for my row? As in something like this....?

$address=$row["address"]str_replace(' ','+',$address);

Any ideas?

Philosopher

5:54 pm on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how about..

$address = str_replace(' ','+',$row["address"]);

If your trying to replace the blanks that are in the variable called from the db, you need to reference that variable.

jspeed

6:01 pm on Jan 10, 2008 (gmt 0)

10+ Year Member



Beautiful, that is exactly what I needed. Thank you!