Example of replacing and filtering all blank characters with spaces in php-PHP source code

Source: Internet
Author: User
Tags php regular expression
There are spaces and ascill spaces in php, so we need to sort them together to replace them, let's take a look at an example of php replacement and filtering of all the blank characters and spaces. In php, there are spaces and ascill spaces. So we need to sort the replacement items together, the following is an example of replacing and filtering all blank characters and spaces with php.

Script ec (2); script

The self-contained trim function in php can only replace spaces on both sides of the left and right. In some cases, it may not be easy to use, if you want to filter out all the spaces (spaces, full-width spaces, line breaks, etc.) in a string, you can write a filter function by yourself.

Php learns str_replace functions and can be replaced in batches. Therefore, we can use the following source code to replace and filter all the blank characters in a string.

Php source code reference:

$ Str = 'jkgsd
Gsgsdgs gsdg gsd ';
Echo myTrim ($ str );
Function myTrim ($ str)
{
$ Search = array ("", "", "\ n", "\ r", "\ t ");
$ Replace = array ("","","","","");
Return str_replace ($ search, $ replace, $ str );
}
?>

Run the code and the page output: jkgsdgsgsdgsgsdggsd, which perfectly achieves the desired effect.

To do this, you can use the PHP regular expression.

In the following example, additional Whitespace can be removed.

$ Str = "This line contains \ tliberal \ r \ n use of whitespace. \ n ";

// First remove the leading/trailing whitespace

// Remove the start and end spaces.

$ 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 ('/[\ n \ r \ t]/', '', $ str );

// Echo out: 'This line contains liberal use of whitespace .'

Echo"

{$str}
";

?>

In this example, extra white space characters are removed.

$ Str = 'foo o ';
$ Str = preg_replace ('/\ s +/', '', $ str );
// It will be changed to 'foo o'
Echo $ str;
?>

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.