php preg_replace 替换换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12498337/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:41:06  来源:igfitidea点击:

php preg_replace replacing line break

phpregex

提问by user61629

When renumbering an array using

使用重新编号数组时

$arr=array_values($arr); // renumber array

I realized that a line break is being introduced into one of the array strings, which I don't want.

我意识到在其中一个数组字符串中引入了换行符,这是我不想要的。

My string is going from:

我的字符串来自:

Property Type

财产种类

to Property

物业

Type

类型

In any case I am using :

无论如何,我正在使用:

$newelement=preg_replace("/[^A-Za-z0-9\s\s+]/"," ",$element); 

already to remove unwanted charchters prior to db insertion so I tried to change it to :

已经在数据库插入之前删除不需要的字符,所以我尝试将其更改为:

 $newelement=preg_replace("/[^A-Za-z0-9\s\s+'<br>''<br>''/n''/cr']/"," ",$element);

But there is no change, and the ?line feed/line break/carriage return is still there.

但是没有变化,?换行/换行/回车仍然存在。

Am I doing the preg_replace correctly?

我是否正确地执行了 preg_replace?

回答by Antti Ryts?l?

That preg looks a bit complicated. And then you have ^ in the beginning as Not A-Z.. or linefeed. So you don't want to replace linefeed?

那个 preg 看起来有点复杂。然后你在开头有 ^ 作为 Not AZ .. 或换行符。所以你不想更换换行符?

How about

怎么样

$newelement=preg_replace("/[\n\r]/","",$element);

or

或者

$newelement=preg_replace("/[^A-Za-z ]/","",$element);

\s also matches linefeed (\n)

\s 也匹配换行符 (\n)

回答by Mahdi

This should work too:

这也应该有效:

// char(32) is whitespace
// for CR
$element = strtr($element, chr(13), chr(32));

// for LF
$element = strtr($element, chr(10), chr(32));

回答by tashi

This thing worked for me.

这件事对我有用。

preg_replace("/\r\n\r\n|\r\r|\n\n/", "<br />", $element);

(Edited to add missing slash)

(编辑以添加缺少的斜线)

回答by Dave Strickler

It's hack, but you can do something like this:

这是黑客,但你可以做这样的事情:

    $email_body_string  = preg_replace("/\=\r\n$/","", $email_body_string);

The replacement says find a line that ends with an equals sign, and has the standard carriage return and line feed characters afterwords. Replace those characters with nothing ("") and the equals sign will disappear, the line below it will be pulled up to join the first line.

替换说找到一个以等号结尾的行,并在后缀中有标准的回车和换行字符。用空字符(“”)替换这些字符,等号将消失,它下面的行将被拉起来加入第一行。

Now, this implies that you will never have a line that end with an equals sign, which is a risk. If you want to do it one better, check the line length where the wrap (with the equals sign) appears. It's usually about 73 characters in from the beginning of the line. Then you could say:

现在,这意味着您永远不会有以等号结尾的行,这是一种风险。如果您想做得更好,请检查出现换行符(带等号)的行长度。它通常是从行首开始的大约 73 个字符。那你可以说:

    if (strlen(equals sign) == 73)
       $email_body_string  = preg_replace("/\=\r\n$/","", $email_body_string);