PHP Replace empty line mismatch empty line problem solution

Source: Internet
Author: User
Tags php code

A blank line, which may include, line breaks, &nbsp, and spaces. When processing a string, it is unavoidable to encounter a blank line processing.
1, replace blank lines, blank lines can include spaces,  ,\t,\f,\n

$s = preg_replace ('/(^ (\s| ) *$)/M ', ', $s);
2, replace blank lines, including line breaks only

$s = preg_replace ('/($\n\r*$) | ( ^\n\r*^)) +/m ', ', $s)
3, replace the blank line, and remove the whitespace from the beginning and end of the Non-empty line

$s = preg_replace ('/^ ( |\s) *| (  |\s) *$/m ', ', $s);
4, does not match the blank line, directly does not match the blank line, is very difficult. Actually does not match the blank line, is matches the Non-empty line

Preg_match_all ('/\s+/m ', $s, $match);
5, multiple-line matching instance

$s =  
 
 test:
 11111
 22222
 
 33333333333333333333 br>  44444444444444444444
  "; 
 
$s = preg_replace ('/(^ (\s|&nbsp;) *$)/M ', ' <br > ', $s); 
 
$pa = "/test: ((\s) *[^<]*) *)/I"; 
Preg_match ($pa, $s, $m); 
 
Echo $m [1]; 
matches the null and void between rows from the test line to the bottom, and is also 11111,22222
6, characters in the common matching pattern
I mode will match both the uppercase and lowercase letters
m strings as multiple lines
S treats the string as a single line, and the newline character as a normal character
X ignores the white space in the pattern by ignoring the
e preg_replace () function to replace the reverse reference in the replacement string, using it as a PHP code evaluation and replacing the searched string with its result.
A forces only the beginning of the target string to match
the $ metacharacters in D mode match only the end of the target string
U matches the nearest string
U pattern string is treated as UTF-8

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.