PHP Regular expressions get rid of line-feed RN

Source: Internet
Author: User
How PHP regular expressions remove line breaks \ r \ n
Now there is this string:
Abcxyz
Qwerty
[ABC]AAAAAAAA
bbbbbbbbb
CCCCCCCCC[/ABC]
Poiuyt
Lkjhgf
[ABC]
Dddddd
Eeeeeee
FFFF[/ABC]
Poiuy
Asdfg

I want to use a regular expression to replace the line break between [ABC] and [/ABC] (that is, \ r \ n). However, the line breaks for strings other than [ABC] and [/ABC] remain unchanged.
The above is an example of this situation [ABC]XXXX[/ABC][ABC] ..., that is, the previous [/ABC] and the latter [ABC] are next to each other.

If you can be compatible with Linux and Windows (line breaks seem different), it's more perfect.

Get a night, ask a group of friends, still no solution, had to trouble everyone.

------Solution--------------------
If not nested:
Preg_replace_callback (' ~\[abc\] (. *?) \[/abc\]~msi ',
function ($matches) {
return Str_replace (Array ("\ r", "\ n"), ", $matches [0]);
},
$text);
------Solution--------------------
PHP code
 $s =<<< Txtabcxyzqwerty[abc] Aaaaaaaabbbbbbbbbccccccccc[/abc]poiuytlkjhgf[abc]ddddddeeeeeeeffff[/abc]poiuyasdfgtxt;echo preg_replace (' #\[abc ].+?\[/ABC] #se ', ' Preg_replace ("/[\r\n]+/", "", "$ A") ', $s);  
------Solution--------------------
discussion

PHP code
$s =<<< TXT
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 Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.