Clear blank strings using regular expressions in PHP _ PHP Tutorial

Source: Internet
Author: User
Tags php regular expression
Use a regular expression in PHP to clear the blank string. You can use the PHP internal function trim () to remove the leading and trailing spaces of the string (). However, we often want to completely clear the blank. You need to clear the white space between start and end. you can use the PHP internal function trim () to remove the white space between start and end of the string (). However, we often want to completely clear the blank. You need to clear the white spaces at the beginning and end, change multiple white spaces to one, and use one rule to process other white spaces of the same type.

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

In the following example, additional Whitespace can be removed.

The code is as follows:


$ 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 the previous example, all spaces are removed step by step. First, we use the trim () function to remove the gaps between start and end. Then, we use preg_replace () to remove duplicates. \ S represents any whitespace. (? =) Indicates forward lookup. It matches only the characters that are followed by the same character as it. Therefore, this regular expression means: "Any whitespace character followed by the whitespace character. "We will replace it with a blank space, so that we can remove it, leaving the only whitespace character.

Finally, we use another regular expression [\ n \ r \ t] to find any residual line breaks (\ n), carriage return (\ r), or tabs (\ t ). We use a space to replace these.

Merge (). However, we often want to completely clear the blank. You need to clear the white space at the beginning and end, and...

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.