cormanaz Posted August 27, 2010 Share Posted August 27, 2010 Good day freaks. I am pulling my hair out trying to figure this out. I am trying to pass patterns for preg_replace through $_GET and then apply them. The object is to highlight certain words in a chunk of text. Say I want to highlight bounded cases of 'foo' in the text. So here's what I'm doing: $string = urldecode($_GET['highlight1']); // $string contains \\bfoo\\b $search = preg_replace('/\\\\b/','b',$string); // $search contains \bfoo\b as I want it to $replace = preg_replace('/\\\\b/','',$string); // $replace contains \foo\ >:-( $pattern = '/'.$search.'/i'; $text = preg_replace($pattern,'<span style="background-color:yellow;">'.$replace.'</span>',$text); The problem is that no matter what I try for the pattern for $replace, it winds up with backslashes around foo. How do I get rid of these? (I should say that I discovered the pattern/replace for the $search string by experimentation and I don't understand why the pattern doesn't pick off all the backslashes in that case--but at least it does what I want). TIA Steve Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 27, 2010 Share Posted August 27, 2010 $string = $_GET['highlight1']; if(get_magic_quotes_gpc()) { $string = stripslashes($string); } Get data is automatically url decoded before the $_GET is populated. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.