Page 1 of 1

Replacing Strings

Posted: Sun Sep 17, 2006 11:18 pm
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?

Posted: Sun Sep 17, 2006 11:26 pm
by Burrito

Posted: Sun Sep 17, 2006 11:31 pm
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..

Posted: Mon Sep 18, 2006 2:03 am
by aaronhall
If you're looking to display all white space:

Code: Select all

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