删除奇怪的换行符 - PHP

时间:2016-10-27 13:56:50

标签: php line-breaks

我有一个看起来完全像这样的字符串:

Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book.

有没有办法让它在PHP中看起来像这样?

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

整句话在一行?段落保留了吗?

更新: 我找到了这个完美的工具。但显然不能使用。 http://www.textfixer.com/tools/remove-line-breaks.php

2 个答案:

答案 0 :(得分:2)

根据您对我的评论的回复,您可能需要尝试换行\r并返回$str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."; // replace all double newlines with breaks $str = str_replace("\n\n", '<br />', $str); // replace all single newlines with a space $str = str_replace("\n", ' ', $str); // replace all breaks with double newlines $str = str_replace('<br />', "\n\n", $str); echo '<pre>'; var_dump($str); echo '<pre>'; ,具体取决于您的实际字符串包含的内容。但以下对我有用:

string(487) "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."

输出:

if (TreeView1.Items.Count > 0)
        (TreeView1.Items[0] as TreeViewItem).IsSelected = true;

答案 1 :(得分:-1)

您可以尝试使用单个空格替换每个多个空格和/或换行符。 它看起来像是:

$string = trim(preg_replace('/\s\s+/', ' ', $string));

希望它有效。