Php regular expression filter space line break press enter

Source: Internet
Author: User
Tags chr php regular expression regular expression

1. The following example can remove extra blank space.

The code is as follows: Copy code

<? Php
$ 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 "<pre >{$ str} </pre> ";
?>

2. Replace line breaks
// Php has three solutions

The code is as follows: Copy code

// 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: Copy code
 
/*
* 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 to <br>

3. Replace the carriage return

The code is as follows: Copy code

<? Php
// 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 Yunqi community editor reminds you that sometimes we also need to pay attention to the need for chr (32), chr (13), and line breaks like/r/n in textarea) this can be replaced.

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.