简体   繁体   English

替换多个换行符,制表符和空格

[英]Replace multiple newlines, tabs, and spaces

I want to replace multiple newline characters with one newline character, and multiple spaces with a single space. 我想用一个换行符替换多个换行符,用一个空格替换多个空格。

I tried preg_replace("/\\n\\n+/", "\\n", $text); 我试过preg_replace("/\\n\\n+/", "\\n", $text); and failed! 失败了!

I also do this job on the $text for formatting. 我也在$ text上进行格式化。

$text = wordwrap($text, 120, '<br/>', true);
$text = nl2br($text);

$text is a large text taken from user for BLOG, and for a better formatting I use wordwrap. $ text是用户为BLOG拍摄的大文本,为了更好的格式化,我使用wordwrap。

In theory, you regular expression does work, but the problem is that not all operating system and browsers send only \\n at the end of string. 从理论上讲,正则表达式确实有效,但问题是并非所有操作系统和浏览器都只在字符串末尾发送\\ n。 Many will also send a \\r. 很多人也会发送\\ r \\ n。

Try: 尝试:

I've simplified this one: 我简化了这个:

preg_replace("/(\r?\n){2,}/", "\n\n", $text);

And to address the problem of some sending \\r only: 并解决了一些发送\\ r的问题:

preg_replace("/[\r\n]{2,}/", "\n\n", $text);

Based on your update: 根据您的更新:

// Replace multiple (one ore more) line breaks with a single one.
$text = preg_replace("/[\r\n]+/", "\n", $text);

$text = wordwrap($text,120, '<br/>', true);
$text = nl2br($text);

Use \\R (which represents any line ending sequence): 使用\\ R(表示任何行结束序列):

$str = preg_replace('#\R+#', '</p><p>', $str);

It was found here: Replacing two new lines with paragraph tags 它在这里找到: 用段落标记替换两个新行

PHP documentation about Escape sequences : 有关Escape序列的 PHP文档:

\\R (line break: matches \\n, \\r and \\r\\n) \\ R(换行符:匹配\\ n,\\ r和\\ r \\ n)

This is the answer, as I understand the question: 这是答案,因为我理解这个问题:

// Normalize newlines
preg_replace('/(\r\n|\r|\n)+/', "\n", $text);
// Replace whitespace characters with a single space
preg_replace('/\s+/', ' ', $text);

This is the actual function that I use to convert new lines to HTML line break and paragraph elements: 这是我用来将新行转换为HTML换行符和段落元素的实际函数:

/**
 *
 * @param string $string
 * @return string
 */
function nl2html($text)
{
    return '<p>' . preg_replace(array('/(\r\n\r\n|\r\r|\n\n)(\s+)?/', '/\r\n|\r|\n/'),
            array('</p><p>', '<br/>'), $text) . '</p>';
}

You need the multiline modifier to match multiple lines: 您需要多行修改器来匹配多行:

preg_replace("/PATTERN/m", "REPLACE", $text);

Also in your example you seem to be replacing 2+ newlines with exactly 2, which isn't what your question indicates. 同样在您的示例中,您似乎正在用2替换2个换行符,这不是您的问题所指示的。

I tried all of above, but it didn't work for me. 我尝试了以上所有,但它对我不起作用。 Then I created some long way to resolve that issue... 然后我创建了一些很长的路来解决这个问题......

Before : 之前:

echo nl2br($text);

After : 之后:

$tempData = nl2br($text);
$tempData = explode("<br />",$tempData);

foreach ($tempData as $val) {
   if(trim($val) != '')
   {
      echo $val."<br />";
   }
}

And it's worked for me.. I wrote here because, if somebody came here to find answer like me. 它对我有用..我写在这里是因为,如果有人来这里寻找像我一样的答案。

如果您只想用单个选项卡替换多个选项卡,请使用以下代码。

preg_replace("/\s{2,}/", "\t", $string);

I would suggest something like this: 我会建议这样的事情:

preg_replace("/(\R){2,}/", "$1", $str);

This will take care of all the Unicode newline characters. 这将处理所有Unicode换行符。

更换头部和字符串或文档的末尾!

preg_replace('/(^[^a-zA-Z]+)|([^a-zA-Z]+$)/','',$match);

I have dealt with strip_tags function in PHP and had some problems like: after having a linebreak then appear a new line with some spaces and then a new linebreak appear continuously ...etc. 我已经在PHP中处理了strip_tags函数并遇到了一些问题,例如:在有了一个换行后,然后出现一个带有一些空格的新行,然后连续出现一个新的换行符......等等。 without any rule :(. 没有任何规则:(。

This is my solution for dealing with strip_tags 这是我处理strip_tags的解决方案

Replace multiple spaces to one, multiple linebreaks to single linebreak 将多个空格替换为一个,多个换行符替换为单个换行符

function cleanHtml($html)
{
    // Clean code into script tags
    $html = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $html);

    // Clean code into style tags
    $html = preg_replace('/<\s*style.+?<\s*\/\s*style.*?>/si', '', $html );

    // Strip HTML
    $string = trim(strip_tags($html));

    // Replace multiple spaces on each line (keep linebreaks) with single space
    $string = preg_replace("/[[:blank:]]+/", " ", $string); // (*)

    // Replace multiple spaces of all positions (deal with linebreaks) with single linebreak
    $string = preg_replace('/\s{2,}/', "\n", $string); // (**)
    return $string;
}

Keywords are (*) and (**). 关键字是(*)和(**)。

尝试这个:

preg_replace("/[\r\n]*/", "\r\n", $text); 

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

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