简体   繁体   English

PHP Regex换行符不会从导入的Facebook描述数据中删除

[英]PHP Regex line-breaks won't be removed from imported Facebook description data

I'm on the edge of going crazy here. 我在这里疯了。 I'm trying to remove line-endings from a string in php, but no existing method seems to work and I can't get my head around why not. 我正在尝试从php中的字符串中删除行尾,但是似乎没有现有的方法起作用,我无法理解为什么不这样做。

I've tried the following methods: 我尝试了以下方法:

$result = str_replace(array("\n", "\r"), '', $string));
$result = preg_replace("/\n/, '', $string"));
$result = preg_replace("/\n|/r|/s/, '', $string")); // this one does remove the whitespace though
$result = str_replace(PHP_EOL, '',trim(rtrim($string)));

And many, many more variations... 还有很多更多的变化...

This makes me think that it might be something else causing the problem, because my test is simple. 这使我认为可能是导致问题的其他原因,因为我的测试很简单。

var_dump($originalString);
$test = " stringl \n";
$testWithoutLineBreak = preg_replace("/\n/", '', $test);
$originalString = preg_replace("/\n/", '', $originalString);
var_dump($test);
var_dump($testWithoutLineBreak);
var_dump($originalString);

Gives the following result: 给出以下结果:

string(13) " stringl
"
string(10) " stringl 
"
string(9) " stringl "
string(13) " stringl
"

Notice the number difference, the test string I made to replicate the original contains 10 characters with the linebreak, while the original string contains 13. Also the preg_replace works on my test string but not on the original. 注意数字上的差异,我用来复制原始字符串的测试字符串包含10个带换行符的字符,而原始字符串包含13个字符。preg_replace也适用于我的测试字符串,但不适用于原始字符串。

Lastly I tried putting it all in a hidden char revealer: 最后,我尝试将所有内容放入隐藏的char揭露程序中:

string(13)[Space]"[Space]stringl[End of Line(LF)]
"[End of Line(LF)]
string(10)[Space]"[Space]stringl[Space][End of Line(LF)]
"[End of Line(LF)]
string(9)[Space]"[Space]stringl[Space]"[End of Line(LF)]
string(13)[Space]"[Space]stringl[End of Line(LF)]
"

No result there. 没有结果。

Anyone an explantion for this magic? 有人为这个魔术着迷吗? Thanks! 谢谢!

Sometime in these situations you get tricked by characters/byte values you can't “see” when viewing the string as plain text or HTML. 在这种情况下,有时您会被字符/字节值所迷惑,当以纯文本或HTML格式查看字符串时,看不到“字符”。

You can for example use urlencode to make a debug output of the value, so that you can determine what the actual byte values at those positions are. 例如,您可以使用urlencode对值进行调试输出,以便可以确定这些位置处的实际字节值。

That usually helps narrow problems like this down. 这通常有助于缩小此类问题的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM