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?
Pass the delimiter to the second argument of the preg_quote
echo preg_replace('/'.preg_quote("$my_str",'~').'/i', '',$string );
^
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);
preg_quotepreg_quote? You may need to add a delimiter.