JS regular expression using regular expressions in PHP to clear the string's whitespace

Source: Internet
Author: User
If you want to remove whitespace from the beginning and end of the string, you can use the PHP internal function trim (). However, we often want to completely clear the blanks. You need to clear the opening and closing blanks, turn multiple whitespace into a blank, and use a rule to handle other whitespace of the same type.
Complete these can be done using a regular expression of PHP
The following example can remove additional whitespace

Copy the Code code as follows:


$STR = "This line contains\tliberal \ r \ n use of whitespace.\n\n";
First remove the leading/trailing whitespace
Remove the opening and closing blanks
$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 ('/[\n\r\t]/', ' ', $str);
Echo out: ' This line contains liberal use of whitespace. '
echo "

{$STR}
";
?>


The previous example removes all the blanks step-by-step. First we use the trim () function to remove the opening and closing blanks. Then we use Preg_replace () to remove the duplicates. \s represents any whitespace. (? =) indicates a forward lookup. It tastes like a character that matches only the characters that follow the same character as itself. So the regular expression means: "Any whitespace character followed by the whitespace character." "We replace them with blanks, so we remove them, leaving the only whitespace characters."
Finally, we use another regular expression [\n\r\t] to find any remaining newline characters (\ n), carriage return (\ r), or tab (\ t). We replace these with a space.

The above describes the JS regular expression in PHP with regular expressions to clear the blank string, including the JS regular expression aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.