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

戏子无情 提交于 2020-01-05 05:48:06

问题


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.


回答1:


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_*




回答2:


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




回答3:


Using stripslashes($str) will not always remove ALL \s from a string. If there is an instance where an \\ exists, it will become one slash, not 0. To get rid of them all, the best thing maybe to be to use str_replace.

And also as stated by the OP, he wishes to remove /" which will need to be done using str_replace



来源:https://stackoverflow.com/questions/13904014/remove-all-backslashes-from-a-string-php-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!