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 "','','