rn怎样在PHP的正则表达式中匹配到?

WBOY
Release: 2016-06-23 13:11:20
Original
1743 people have browsed it

例如这么一串小字符串,\r\n
如何匹配到\r\n?
(\\r\\n)+好像不行?


回复讨论(解决方案)

“/[\r\n]+/" 一定要放在双引号中,否则 \r\n 会被解释为 \、r、\、n
或 '/[\x0d\x0a]+/'

我单独做了个字符串试了下,确实是可以,但是我遇到的问题是:
我是从数据库中取出的一个字符串,这串字符串中包含\r\n这样的换行符,直接输出到浏览器是原样输出,我想用preg_replace用
替换这两个字符,不管怎样都无法提取到这两个字符,像"/(\\r\\n)+/"或者如你所说的那样替换,都无法匹配到。字符串录入数据库时已经做过转义,肯定版主解答下这是什么原因?非常感谢

“/[\r\n]+/" 一定要放在双引号中,否则 \r\n 会被解释为 \、r、\、n
或 '/[\x0d\x0a]+/'


我单独做了个字符串试了下,确实是可以,但是我遇到的问题是:
我是从数据库中取出的一个字符串,这串字符串中包含\r\n这样的换行符,直接输出到浏览器是原样输出,我想用preg_replace用
替换这两个字符,不管怎样都无法提取到这两个字符,像"/(\\r\\n)+/"或者如你所说的那样替换,都无法匹配到。字符串录入数据库时已经做过转义,肯定版主解答下这是什么原因?非常感谢

echo base64_encode(你直接从数据库里独处的内容);
贴出结果

转 

php 提供有 nl2br 函数,就是干这个这个事情的

echo base64_encode(你直接从数据库里独处的内容);
贴出结果

转 

php 提供有 nl2br 函数,就是干这个这个事情的



nl2br试过,没有作用,因为被转义成了 \\r\\n
我用str_replace("\\r\\n","
",$string);是可以成功替换的达到要求的。
 base64_encode输出结果:
W3NpemU9MjBdMjBweOeahOWtl+S9k1svc2l6ZV1cclxuW2Jd5paH5a2X5Yqg57KXWy9iXVxyXG5baV3lgL7mlpxbL2ldXHJcblt1XeS4i+WIkue6v1svdV1cclxuW3Nd5Lit5YiS57q/Wy9zXVxyXG5bY29sb3I9I2YwMF3nuqLoibJbL2NvbG9yXVxyXG5bdXJsXWh0dHA6Ly93d3cuMTYzLmNvbVsvdXJsXVxyXG5bZW1haWxdcmVkXzAyNkAxNjMuY29tWy9lbWFpbF1cclxuW2ltZ11xcGljLzMvMzAuZ2lmWy9pbWdd

$s = 'W3NpemU9MjBdMjBweOeahOWtl+S9k1svc2l6ZV1cclxuW2Jd5paH5a2X5Yqg57KXWy9iXVxyXG5baV3lgL7mlpxbL2ldXHJcblt1XeS4i+WIkue6v1svdV1cclxuW3Nd5Lit5YiS57q/Wy9zXVxyXG5bY29sb3I9I2YwMF3nuqLoibJbL2NvbG9yXVxyXG5bdXJsXWh0dHA6Ly93d3cuMTYzLmNvbVsvdXJsXVxyXG5bZW1haWxdcmVkXzAyNkAxNjMuY29tWy9lbWFpbF1cclxuW2ltZ11xcGljLzMvMzAuZ2lmWy9pbWdd';$s = base64_decode($s);echo nl2br(stripcslashes($s));
Copy after login
Copy after login
20px的字体<br /><strong>文字加粗</strong><br />倾斜<br />下划线<br />中划线<br />[color=#f00]红色[/color]<br />http://www.163.com<br />red_026@163.com<br />
Copy after login
Copy after login

$s = 'W3NpemU9MjBdMjBweOeahOWtl+S9k1svc2l6ZV1cclxuW2Jd5paH5a2X5Yqg57KXWy9iXVxyXG5baV3lgL7mlpxbL2ldXHJcblt1XeS4i+WIkue6v1svdV1cclxuW3Nd5Lit5YiS57q/Wy9zXVxyXG5bY29sb3I9I2YwMF3nuqLoibJbL2NvbG9yXVxyXG5bdXJsXWh0dHA6Ly93d3cuMTYzLmNvbVsvdXJsXVxyXG5bZW1haWxdcmVkXzAyNkAxNjMuY29tWy9lbWFpbF1cclxuW2ltZ11xcGljLzMvMzAuZ2lmWy9pbWdd';$s = base64_decode($s);echo nl2br(stripcslashes($s));
Copy after login
Copy after login
20px的字体<br /><strong>文字加粗</strong><br />倾斜<br />下划线<br />中划线<br />[color=#f00]红色[/color]<br />http://www.163.com<br />red_026@163.com<br />
Copy after login
Copy after login



非常非常感谢!
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!