I want to replace linebreaks with ' '
in PHP. Somehow I can't get it to work on this json encoded string [[0,"Hello World"],[1,"s\n"]]
with $x = preg_replace('/\r\n|\r|\n\r|\n/m', ' ', $x);
.
我想在PHP中用''替换换行符。不知怎的,我无法让它在这个json编码的字符串[[0,“Hello World”],[1,“s \ n”]]上工作,其中$ x = preg_replace('/ \ r \ n | \ r | \ n \ r | \ n / m','',$ x);.
I'm out of ideas. And i know that the php code works with none-json encoded strings. Any ideas to fix this problem
我没有想法。我知道php代码适用于非json编码的字符串。任何想法来解决这个问题
Forgot this:
When I input the string as $x
the function or php code returns the same string. Instead of replacing \n
with ' '
.
当我输入字符串为$ xthe函数或php代码返回相同的字符串。而不是用''替换\ n。
I have also tried all relevant problems in *. none of them successful
我还尝试了*中的所有相关问题。他们都没有成功
1 个解决方案
#1
1
preg_replace will try to parse the '\n' as an actual newline character, so you need some more escaping in there.
preg_replace将尝试将'\ n'解析为实际的换行符,因此您需要在那里进行一些转义。
$x = preg_replace('/\\\r\\\n|\\\r|\\\n\\\r|\\\n/m', ' ', $x);
This is all kind of ugly though. Is there a reason you can't do a replace in the actual decoded strings instead?
这有点难看。您是否有理由不能在实际解码的字符串中进行替换?
#1
1
preg_replace will try to parse the '\n' as an actual newline character, so you need some more escaping in there.
preg_replace将尝试将'\ n'解析为实际的换行符,因此您需要在那里进行一些转义。
$x = preg_replace('/\\\r\\\n|\\\r|\\\n\\\r|\\\n/m', ' ', $x);
This is all kind of ugly though. Is there a reason you can't do a replace in the actual decoded strings instead?
这有点难看。您是否有理由不能在实际解码的字符串中进行替换?