How to remove unnecessary spaces in regular expressions-php Tutorial

Source: Internet
Author: User
How can I remove unnecessary spaces in a regular expression? Echo & nbsp; $ keyword & nbsp; Shanghai | Beijing! Chengdu & nbsp; Shenzhen; echo & nbsp; & lt; br & nbsp; & gt; & nbsp; $ old1 & nbsp; array (s *) (s) (s *), (s *) (|) (s *), (s *)(!) (How to remove extra spaces in the s regular expression?

Echo $ keyword = "Shanghai | Beijing! Chengdu, Shenzhen ";
Echo"
";
$ Old1 = array ("/(\ s *)/", "/(\ s *) (\ |) (\ s *) /","/(\ s *)(!) (\ S *)/");
$ New1 = array ('"', '" | "','""! ');
Echo $ keyword3 = "\" ". preg_replace ($ old1, $ new1, $ keyword )."\"";
// Output: "Shanghai" | "Beijing ""! Chengdu "" Shenzhen"

Echo $ keyword = "Shanghai | Beijing! Chengdu, Shenzhen ";
Echo"
";
$ Old1 = array ("/(\ s *)/", "/(\ s *) (\ |) (\ s *) /","/(\ s *)(!) (\ S *)/");
$ New1 = array ('"', '" | "','""! ');
Echo $ keyword3 = "\" ". preg_replace ($ old1, $ new1, $ keyword )."\"";
// Output: "Shanghai" | "" Beijing """"! Chengdu "" Shenzhen"

In the preceding two cases, the first case is that the display is correct, and the second case is that it is wrong to add a space to each symbol. How can I block unnecessary spaces?

Regular expression


------ Solution --------------------
I don't understand your needs. I can use \ S for extraction.
------ Solution --------------------
Can the following practices meet your needs?


Echo $ keyword = "Shanghai
------ Solution --------------------
Beijing! Chengdu, Shenzhen ";
Echo"
";
$ Old1 = array ("/(\ s *)/", "/(\ s *)(\
------ Solution --------------------
) (\ S *)/","/(\ s *)(!) (\ S *)/");
$ New1 = array ('""','"
------ Solution --------------------
"','""! ');
Echo $ keyword3 = "\" ". preg_replace ($ old1, $ new1, $ keyword )."\"";
// Output: "Shanghai"
------ Solution --------------------
"Beijing ""! Chengdu "" Shenzhen"

Echo $ keyword = "Shanghai
------ Solution --------------------
Beijing! Chengdu, Shenzhen ";
$ Keyword = str_replace ("", "", $ keyword );
Echo"
";
$ Old1 = array ("/(\ s *)/", "/(\ s *)(\
------ Solution --------------------
) (\ S *)/","/(\ s *)(!) (\ S *)/");
$ New1 = array ('""','"
------ Solution --------------------
"','""! ');
Echo $ keyword3 = "\" ". preg_replace ($ old1, $ new1, $ keyword )."\"";
// Output: "Shanghai """
------ Solution --------------------
"Beijing """"! Chengdu "" Shenzhen"

------ Solution --------------------
Write in this way
$ Keyword = "Shanghai
------ Solution --------------------
Beijing! Chengdu, Shenzhen "; // output:" Shanghai"
------ Solution --------------------
"Beijing ""! Chengdu "" Shenzhen"
$ Keyword = "Shanghai
------ Solution --------------------
Beijing! Chengdu, Shenzhen "; // output:" Shanghai"
------ Solution --------------------
"Beijing ""! Chengdu "" Shenzhen"
Echo"
";
$ Old1 = array ("/!? [^ \ S!
------ Solution --------------------
] +/","/\ S +/","/\ s *\
------ Solution --------------------
\ S */",'/""/');
$ New1 = array ('"$0 "','','

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.