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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.