Forum Moderators: coopster

Message Too Old, No Replies

preg_replace (but not quite everything)

         

Patrick Taylor

2:27 pm on Aug 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm using this as part of a form input:


// Remove js, HTML tags, and white space.
$search = array ("'<script[^>]*?>.*?</script>'si", "'<[\/\!]*?[^<>]*?>'si", "'([\r\n])[\s]+'");
$replace = array ("", "", "\\1");
$mytext = preg_replace($search, $replace, $mytext);

It works perfectly, but I would like to allow the user to use a break tag: <br /> without it being removed - is there a way, without completely throwing out the above?

Hester

10:04 pm on Aug 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Convert all break tags first to a special character. (Make sure your users can't enter the character by replacing any occurences of it with a blank first.) Then just convert that back to break tags afterwards.

Eg:


$text = str_replace('¬', '', $text); //remove char from user text
$text = str_replace('<br />', '¬', $text); //replace break tags with funny character
...your regex code here...
$text = str_replace('¬', '<br />', $text); //now restore break tags