Replace a space with an underscore SOLVED

Reply

Join Date: Nov 2007
Posts: 86
Reputation: sagedavis is an unknown quantity at this point 
Solved Threads: 6
sagedavis sagedavis is offline Offline
Junior Poster in Training

Replace a space with an underscore SOLVED

 
0
  #1
Feb 18th, 2008
Sometimes it's just something so simple.
I have been racking my brain, searching the net, for, litterally, 6 hours. Looking to do the following.
If the user puts "this phrase" into the form field, it will send to the database "this_phrase".
Hours and hours of research, solutions using preg_replace with all sorts of regular expressions, and other regexp functions, which spit out error after error, or got me nowhere near where I wanted to be with this.

Finally, I look up str_replace and behold the pot of gold that made it ever so simple.
I am posting this for those of you who have struggled with this, in hopes that it just might save you those hours.

It was such a simple solution.
  1. <?PHP
  2. $name = $_POST[name];
  3. $pagelink = str_replace(" ","_",$name);
  4. ?>

I do not know why it is, with all of the sites that talk about replacing a space with an underscore, none of them gave this, ever so simple answer. Maybe it just doesn't look as cool as all of that regular expression stuff?

Maybe I just have no google skills?
Anyway, there is a rather huge weight lifted off of my shoulders now.
Sage
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Replace a space with an underscore SOLVED

 
0
  #2
Feb 19th, 2008
I do not know why it is, with all of the sites that talk about replacing a space with an underscore, none of them gave this, ever so simple answer.
What did you search for
Maybe it just doesn't look as cool as all of that regular expression stuff?
No. regular expressions are slower than string functions. Regular expressions are preferred only for complicated string matching and stuff.
Last edited by nav33n; Feb 19th, 2008 at 12:07 am.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 86
Reputation: sagedavis is an unknown quantity at this point 
Solved Threads: 6
sagedavis sagedavis is offline Offline
Junior Poster in Training

Re: Replace a space with an underscore SOLVED

 
0
  #3
Feb 19th, 2008
Nav33
Hey bud... :-)
I can't even remember what all I searched for now...
I am not good at regular expressions anyway, lol so, I'm ok with trying to work around them.
Thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Replace a space with an underscore SOLVED

 
0
  #4
Dec 28th, 2008
hi, I do not have any special PHP knowledge, so I would like someone to give me a code for this; I have a "text.txt" file on my server, which has underscores in it. I want a PHP code which replaces all the underscores with spaces and then save the txt. thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC