Jump to content

How to get rid of backslashes passed through $_GET


cormanaz

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.