我想问一个与上一个问题(Remove new lines from string)略有不同的变化:
我想删除字符串中的新行,除了,当新行上的文本前面有一个空格“”时(这表示这是一个新的段落。这是一个例子:
This is the first bit of text. It continues for a while until
there is a line break. It then continues for a bit longer but
the line break also continues alongside it.
What you see here is the second paragraph. You'll notice that
there is a space to mark the beginning of the paragraph. However
When joining these lines, a computer may not realize that the next
paragraph exists in this way.
答案 0 :(得分:3)
$result = preg_replace('/\n++(?! )/', ' ', $subject);
正是如此。
<强>说明:强>
\n++ # Match one or more newlines; don't backtrack
(?! ) # only if it's impossible to match a space afterwards
答案 1 :(得分:0)
按终点字符分解文本(使用PHP_EOL常量)并使用trim。
$lines = explode(PHP_EOL,$text);
$lines = array_map(function($line){
return trim($line);
},$lines);
$text = implode(PHP_EOL,$lines);
// or if you are not familiar w/ array_map, simple use foreach
$lines = array();
foreach(explode(PHP_EOL,$text) as $line)
$lines[] = trim($line);
$text = implode(PHP_EOL,$lines);
答案 2 :(得分:0)
在您的文件中看起来像嵌入式小端BOM。
必须删除它们或在没有它们的情况下重写文件。
您可以使用像\xFF\xFE
这样的正则表达式删除它们。
以下是十六进制编辑器中文本的一部分。
00 61 00 6C 00 6F 00 6E 00 67 00 73 00 69 00 64
00 65 00 20 00 69 00 74 00 2E 00 20 00 0D 00 20
00 FF FE 20 00 FF FE 20 00 FF FE 57 00 68 00 61
00 74 00 20 00 79 00 6F 00 75 00
alongside it.
What you