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

发布于 2024-11-15 11:35:34 字数 295 浏览 3 评论 0原文

我想用一个换行符替换多个换行符,用一个空格替换多个空格。

我尝试了 preg_replace("/\n\n+/", "\n", $text); 但失败了!

我还在 $text 上完成这项工作以进行格式化。

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

$text 是从 BLOG 用户那里获取的大文本,为了更好的格式化,我使用了 wordwrap。

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); and failed!

I also do this job on the $text for formatting.

$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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

妄断弥空 2024-11-22 11:35:34

理论上,正则表达式确实有效,但问题是并非所有操作系统和浏览器仅在字符串末尾发送 \n 。许多人还会发送\r。

尝试:

我简化了这一点:

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

并解决某些仅发送 \r 的问题:

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

根据您的更新:

// 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);

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. Many will also send a \r.

Try:

I've simplified this one:

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

And to address the problem of some sending \r only:

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);
治碍 2024-11-22 11:35:34

使用\R(代表任何行结束序列):

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

它在这里找到:用段落标签替换两行

关于 转义序列

\R(换行符:匹配 \n、\r 和 \r\n)

Use \R (which represents any line ending sequence):

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

It was found here: Replacing two new lines with paragraph tags

PHP documentation about Escape sequences:

\R (line break: matches \n, \r and \r\n)

深陷 2024-11-22 11:35:34

这就是答案,正如我理解的问题:

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

这是我用来将新行转换为 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>';
}

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:

/**
 *
 * @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>';
}
我爱人 2024-11-22 11:35:34

您需要多行修饰符来匹配多行:

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

另外,在您的示例中,您似乎将 2 个以上换行符替换为 2 个换行符,这不是您的问题所表明的。

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.

鹤仙姿 2024-11-22 11:35:34

我尝试了以上所有方法,但对我来说没有用。然后我创建了一些很长的方法来解决这个问题......

之前:

echo nl2br($text);

之后:

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

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

它对我有用......我写在这里是因为,如果有人像我一样来这里寻找答案。

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.

生来就爱笑 2024-11-22 11:35:34

我建议这样:

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

这将处理所有 Unicode 换行符。

I would suggest something like this:

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

This will take care of all the Unicode newline characters.

甩你一脸翔 2024-11-22 11:35:34

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

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

If you just want to replace multiple tabs with a single tab, use the following code.

preg_replace("/\s{2,}/", "\t", $string);
眼前雾蒙蒙 2024-11-22 11:35:34

试试这个:

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

Try this:

preg_replace("/[\r\n]*/", "\r\n", $text); 
心房敞 2024-11-22 11:35:34

替换字符串或文档的头部和结尾!

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

Replace the head and the end of string or document!

preg_replace('/(^[^a-zA-Z]+)|([^a-zA-Z]+$)/','',$match);
过期以后 2024-11-22 11:35:34

我在 PHP 中处理过 strip_tags 函数,遇到了一些问题,例如:在换行之后出现一个带有一些空格的新行,然后连续出现一个新的换行符......等等。没有任何规则:(。

这是我处理 strip_tags 的解决方案

将多个空格替换为一个,将多个换行符替换为单个换行符

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;
}

关键字是 (*) 和 (**)。

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. without any rule :(.

This is my solution for dealing with 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 (**).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文