I've to replace newline (\\n) with & in a string so that the received data could be parsed with parse_str() into array. The thing is that when I put \\n in single quote it somehow turns out as to be replaced with a space:
str_ireplace(array('&', '+', '\n'), array('', '', '&'), $response)
"id=1 name=name gender=gender age=age friends=friends"
But when I put \\n in double quotes then it works just fine:
str_ireplace(array('&', '+', "\n"), array('', '', '&'), $response)
"id=1&name=name&gender=gender&age=age&friends=friends"
Why is that so?
Because only the escaped sequences \\'
and \\\\
have a meaning in single quoted strings.
See the documentation :
To specify a literal single quote, escape it with a backslash (
\\
). To specify a literal backslash, double it (\\\\
). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as\\r
or\\n
, will be output literally as specified rather than having any special meaning.
Update:
Another difference is that PHP only substitutes variables inside double-quoted strings (and heredoc) . Therefore you can consider processing of single-quoted strings to be faster in general (but maybe not measurably faster).
Btw you don't necessarily need to use str_ireplace
as &
, +
and \\n
have no upper or lower case version. There is just one version, so str_replace
would be enough.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.