php 替换多个换行符、制表符和空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6360566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace multiple newlines, tabs, and spaces
提问by Sourav
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!
我试过了preg_replace("/\n\n+/", "\n", $text);
,失败了!
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 的大文本,为了更好的格式,我使用自动换行。
回答by Francois Deschenes
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.
理论上,您的正则表达式确实有效,但问题是并非所有操作系统和浏览器都只在字符串末尾发送 \n。许多人还会发送一个\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:
并解决一些只发送 \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);
回答by Armel Larcier
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:
关于转义序列的PHP 文档:
\R (line break: matches \n, \r and \r\n)
\R(换行符:匹配 \n、\r 和 \r\n)
回答by Sonny
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>';
}
回答by Chris Eberle
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 个,这不是您的问题所指的内容。
回答by Shaunak Shukla
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.
它对我有用.. 我写在这里是因为,如果有人像我一样来这里寻找答案。
回答by Heman G
If you just want to replace multiple tabs with a single tab, use the following code.
如果您只想用单个选项卡替换多个选项卡,请使用以下代码。
preg_replace("/\s{2,}/", "\t", $string);
回答by anubhava
I would suggest something like this:
我会建议这样的事情:
preg_replace("/(\R){2,}/", "", $str);
This will take care of all the Unicode newline characters.
这将处理所有 Unicode 换行符。
回答by user3129040
Replace the head and the end of string or document!
替换字符串或文档的头部和结尾!
preg_replace('/(^[^a-zA-Z]+)|([^a-zA-Z]+$)/','',$match);
回答by Hai Nguyen
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 :(.
我处理过 PHP 中的 strip_tags 函数,遇到了一些问题,例如:在换行之后出现一个带有一些空格的新行,然后一个新的换行连续出现......等等。没有任何规则:(。
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 (**).
关键字是 (*) 和 (**)。
回答by The Mask
Try this:
尝试这个:
preg_replace("/[\r\n]*/", "\r\n", $text);