3

I'am trying to pass a string

  preg_replace('/'.preg_quote($my_str).'/i', '', $string);

but I get an error

preg_replace(): Unknown modifier '\'

is there any other solutions to escape those backslashes?

3
  • 2
    You have to pass regexp delimiter (/) as 2nd parameter for preg_quote Commented Apr 12, 2014 at 13:10
  • What is the regular expression you're passing into preg_quote? You may need to add a delimiter. Commented Apr 12, 2014 at 13:11
  • Can you show us the value of $my_str Commented Apr 12, 2014 at 13:15

2 Answers 2

2

Pass the delimiter to the second argument of the preg_quote

echo preg_replace('/'.preg_quote("$my_str",'~').'/i', '',$string );
                                            ^
Sign up to request clarification or add additional context in comments.

Comments

0

Your $my_str contains / character in it which you are using as regex delimiter. So you have two solutions,

One, replace the delimiter / into anything else which doesn't contain in the variable. For example ~

preg_replace('~'.preg_quote($my_str).'~i', '', $string);
              ^                       ^

Second solution, replace all the / with \/ from $my_str after preg_quote

$my_str = str_replace('/', '\/', preg_quote($my_str));
$some_str = preg_replace('/'.$my_str.'/i', '', $string);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.