Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all backslashes from a string - php - regex [closed]

my string is

$str = '<img src="\"images/hai.jpg\"" alt="" /> Text <img src="\"images/hai.jpg\"" alt="" />';

i want to remove all \" from the string.

like image 311
FrancisMV123 Avatar asked Feb 21 '26 10:02

FrancisMV123


2 Answers

I think you aren't looking for a regex, but for the stripslashes($str) method.

EDIT: From the comments, I understand that you will only replace \" with nothing, you should use a simple str_replace here, as @Gumbo said:

$str = ...;
$newStr = str_replace('\"', '', $str);

echo $newStr;

You can use regular expressions for this, but the pReg library isn't fast, if you can find a str_* or array variant which does the same I always recommend to use that instead of preg_*

like image 68
Wouter J Avatar answered Feb 23 '26 22:02

Wouter J


This looks like the sort of data string that has been through more than one escape sequence. You might want to look for the underlying cause of the backslashes and duplicated quotes. In PHP, magic quotes can cause this sort of thing. If you can get to this article it will explain the issues.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_6630-Magic-Quotes-a-bad-idea-from-day-one.html

HTH, ~Ray

like image 38
Ray Paseur Avatar answered Feb 23 '26 22:02

Ray Paseur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!