Replace "%20" with a space in a string!

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
VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Replace "%20" with a space in a string!

Post by VKX »

I have a string, lets say $username. I want to go through that string and replace all the "%20" with space so something like post%20a%20new%20topic would turn into post a new topic.

Just point me towards a function, and I'll see if I can do it from there :)
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

str_replace()

Its a pretty simple function, if you need help come back and post. :D

Code: Select all

$username = "post%20a%20new%20topic";

$new = str_replace("%20"," ", $username);

// $new would echo out to be "post a new topic"
Last edited by nickman013 on Sun Feb 26, 2006 10:45 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

you pretty much solved your own question in your topic title ;)

replace .... string :arrow: str_replace()

or use urldecode()
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Post by VKX »

Works great, but what if I just want to delete a space all together? ie:

some var with spaces = somevarwithspaces
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Same thing pretty much.

Code: Select all

$blah = "this has spaces";

str_replace(" ", "", $blah);
//$blah will echo out with no spaces now
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

empty string for the second parameter...
VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Post by VKX »

Works great, thanks again guys.
Post Reply