Forum Moderators: coopster

Message Too Old, No Replies

URL-Friendly Strings

How To Replace Spaces with "+"

         

inuwolf

2:48 pm on Feb 17, 2006 (gmt 0)

10+ Year Member



My form uses strings to call fields from a CSV file. Often this fields are inserted directly into a URL, like "http://example.com?=$field". If the field has more than one word, like "John Smith", the URL will come out "http://example.com?=John". How do I replace spaces in fields with something like "+" or "%20"?

I don't want to replace the space in the database itself, because not all the fields are called in URLS. Some want to be read as "John Smith" on the screen. I'm sure there are many many ways to do this, but I'd like an efficient one, as there will be a LOT of URLS on my page.

omoutop

3:11 pm on Feb 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



lets say that theoriginal var is $first_var.

$new_var = str_replace(" ", "_", $first_var) in which case the $new_var has all spaces replaced by (_) underscrores

good luck

coopster

4:45 pm on Feb 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Or you could urlencode [php.net] the string.

inuwolf

5:06 pm on Feb 17, 2006 (gmt 0)

10+ Year Member



thanks coopster, that's exactly what I'm looking for. and omoutop, your little snippet will help me transfer information in the URL back into readable form. thanks