PHP Regular expression filter space line break Enter _php tutorial

Source: Internet
Author: User
One, the following example can remove additional whitespace

The code is as follows

$STR = "This line containstliberal RN use of whitespace.nn";
First remove the leading/trailing whitespace
Remove the opening and closing blanks www.111cn.net
$str = Trim ($STR);
Now remove any doubled-up whitespace
Remove the blank that follows the other squeeze
$str = Preg_replace ('/s (? =s)/', ' ', $str);
Finally, replace any non-space whitespace, with a space
Finally, remove the non-space space, with a space instead
$str = preg_replace ('/[nrt]/', ' ', $str);
Echo out: ' This line contains liberal use of whitespace. '
echo "

{$STR}
";
?>

Second, replace the line break

PHP has three ways to solve

The code is as follows

1. Use Str_replace to replace line breaks
$str = Str_replace (Array ("RN", "R", "N"), "", $str);

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

3. Use PHP-defined variables (recommended)
$str = Str_replace (Php_eol, ", $STR);

The code is as follows:

The code is as follows

/*
* Get the user's operating system line break, 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 ';//Right important point www.111cn.net
}
return $the _crlf;
}

Note: When the foreground page is displayed, use NL2BR to change the line to

Third, replace the carriage return

The code is as follows

PHP line breaks for different systems
The implementation of line breaks between different systems is not the same
Linux and Unix with/n
MAC with/R
Window is/r/n in order to show that it is different from Linux
So the implementation method on different platforms is not the same
PHP has three ways to solve

1. Use Str_replace to replace line breaks
$str = Str_replace (Array ("/r/n", "/R", "/n"), "", $str);

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

3. Use PHP-defined variables (recommended)
$str = Str_replace (Php_eol, ", $STR);
?>

http://www.bkjia.com/PHPjc/727587.html www.bkjia.com true http://www.bkjia.com/PHPjc/727587.html techarticle One, the following example can remove the additional blank code as follows? PHP $str = This line containstliberal rn use of whitespace.nn;//First remove the leading/trailing whitespace//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.