JS under the regular is this:
var str = "data\/ui_inner\/74\/";str.replace(/\\/,"");
PHP does not write as follows:
$auto_url = preg_replace('/\\/','', $str);
My goal is to replace \ Empty in the string, how can I replace it?
According to the answer downstairs to try or not.
Find the reason, is because Json_encode () caused, how to solve it? I'm going to Google.
Reply content:
JS under the regular is this:
var str = "data\/ui_inner\/74\/";str.replace(/\\/,"");
PHP does not write as follows:
$auto_url = preg_replace('/\\/','', $str);
My goal is to replace \ Empty in the string, how can I replace it?
According to the answer downstairs to try or not.
Find the reason, is because Json_encode () caused, how to solve it? I'm going to Google.
Unless the landlord is neat, it is not recommended to go to json_encode
replace the results, because json_encode
the /
escape output, for JS, its content is /
, and not \/
.
(PS: The following in the test when there are comments in the code var_dump
, only reserved echo
)
Replaced by:
Not replaced by:
1.html
The code is as follows:
PHP provides a function preg_quote
string preg_quote ( string $str [, string $delimiter = NULL ] )
Preg_quote () requires the parameter str and adds a backslash to the character in each of the regular expression syntax. This is typically used when you have some run-time strings that need to be matched as regular expressions.
The regular expression special characters are:. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
所以以上代码可以这样写
This should be the case
$auto _url = preg_replace ('/\\\\/', ' ', $str);
The is also just learned, because in the string, ' \ ' is also escaped, '/\\/' becomes '/\/', so to four.
You can also use Str_replace
$auto _url = str_replace (' \ \ ', ' ', $str);
The single quotation mark in PHP will still escape \
So you need to do this
$auto _url = preg_replace ('/\\\\/', ' '," data\/ui_inner\/74\/");
to four \\\\
It is unsafe to do regular replacement of JSON directly, because if the business logic changes, You may have accidentally modified other domains, it is recommended to decode and then encode