Home > Backend Development > PHP Tutorial > php正则表达式怎的去掉换行符rn

php正则表达式怎的去掉换行符rn

WBOY
Release: 2016-06-13 10:52:20
Original
1181 people have browsed it

php正则表达式怎样去掉换行符\r\n
现在有这么一段字符串:
abcxyz
qwerty
[abc]aaaaaaaa
bbbbbbbbb
ccccccccc[/abc]
poiuyt
lkjhgf
[abc]
dddddd
eeeeeee
ffff[/abc]
poiuy
asdfg

我希望用个正则表达式,把[abc]和[/abc]之间的换行符(也就是\r\n)给替换掉。但是[abc]和[/abc]之外的字符串的换行符保留不变。
上面的是个例子,有可能是这种情况[abc]xxxx[/abc][abc]......,也就是前一个[/abc]和后一个[abc]紧挨着。

如果能兼容linux和windows(换行符好像不一样),就更完美了。

弄了一晚上,请教了群友,仍然没有解决,只好来麻烦大家了。

------解决方案--------------------
如果没有嵌套:
preg_replace_callback( '~\[abc\](.*?)\[/abc\]~msi',
function($matches){
return str_replace(array("\r","\n"),'',$matches[0]);
},
$text);
------解决方案--------------------

PHP code
$s =<font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨

PHP code
$s =abcxyz
qwerty
[abc]aaaaaaaa
bbbbbbbbb
ccccccccc[/abc]
poiuyt
lkjhgf
[abc]
dddddd
eeeeeee
ffff[/abc]
poiuy
asdfg
TXT;

echo preg_replace('#\[abc].+?\[/abc]#se', 'preg_replace("/[……
Related labels:
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