Php regular expression filtering space linefeed carriage return _ PHP Tutorial

Source: Internet
Author: User
Tags php regular expression
Use the php regular expression to filter the blank line breaks and press enter. 1. the following example shows how to remove extra blank code? Php $ strThislinecontainstliberalrnuseofwhitespace. nn; Firstremovetheleadingtrailingwhitespace remove start and end 1. the following example can remove extra spaces.

The code is as follows:

$ Str = "This line containstliberal rn use of whitespace. nn ";
// First remove the leading/trailing whitespace
// Remove the start and end blank www.111cn.net
$ Str = trim ($ str );
// Now remove any doubled-up whitespace
// Remove the blank space crowded with other items
$ Str = preg_replace ('/s (? = S)/', '', $ str );
// Finally, replace any non-space whitespace, with a space
// Finally, remove the non-space blank and replace it with a space.
$ Str = preg_replace ('/[nrt]/', '', $ str );
// Echo out: 'This line contains liberal use of whitespace .'
Echo"

{$str}
";
?>

2. replace line breaks

// Php has three solutions

The code is as follows:

// 1. use str_replace to replace the line feed.
$ Str = str_replace (array ("rn", "r", "n"), "", $ str );

// 2. use regular expression replacement
$ Str = preg_replace ('// s */', '', $ str );

// 3. use the variables defined in php (recommended)
$ Str = str_replace (PHP_EOL, '', $ str );

The code is as follows:

The code is as follows:

/*
* Get the line break of the user's operating system, n
* @ Access public
* @ Return string
*/
Function get_crlf ()
{
If (stristr ($ _ SERVER ['http _ USER_AGENT '], 'win '))
{
$ The_crlf = 'RN ';
}
Elseif (stristr ($ _ SERVER ['http _ USER_AGENT '], 'Mac '))
{
$ The_crlf = 'R'; // for old MAC OS
}
Else
{
$ The_crlf = 'n'; // www.111cn.net with a higher weight
}
Return $ the_crlf;
}

Note: Use nl2br to change the line feed

3. replace the carriage return

The code is as follows:

// Line feed for different php systems
// The implementation of line feed varies with systems.
// In linux and unix/n
// For MAC/r
// Window is/r/n to reflect differences from linux
// The Implementation methods on different platforms are different.
// Php has three solutions

// 1. use str_replace to replace the line feed.
$ Str = str_replace (array ("/r/n", "/r", "/n"), "", $ str );

// 2. use regular expression replacement
$ Str = preg_replace ('// s */', '', $ str );

// 3. use the variables defined in php (recommended)
$ Str = str_replace (PHP_EOL, '', $ str );
?>

The pipeline code is as follows? Php $ str = This line containstliberal rn use of whitespace. nn; // First remove the leading/trailingwhitespace // remove start and end...

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.