Replacing Strings

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
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Replacing Strings

Post by watson516 »

I am building a little site and one of the things on it allows some people to give a description to certain items. Type in a text box, submit it, all is well. The only problem I am having is that when the user presses enter or puts in extra spaces and such, it doesn't show up because of the html and whitespace. So, is there some way I can replace all of the enters and extra spaces and such with html tags?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

arkady
Forum Newbie
Posts: 23
Joined: Sun Sep 17, 2006 9:34 pm

Post by arkady »

you could use preg_replace

something along the lines of:

Code: Select all

$string_entered = $_GET['myfieldname'];
if( somefunction_tocheck_textisnt_malicious($string_entered) )
{
  // replace all whitespace with a single html space
  preg_replace('/\s\s+/', '&nbsp', $string_entered);
  // replace all new lines with a BR tag
  preg_replace('/\n/', '<BR>', $string_entered);
}

etc..
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

If you're looking to display all white space:

Code: Select all

<pre><? echo $myString; ?></pre>
Post Reply