How to remove the regular expression extra space?

Source: Internet
Author: User
echo $keyword = "Shanghai | Beijing, Chengdu, Shenzhen"; echo "
"; $old 1 = Array ("/(\s*) (\s) (\s*)/", "/(\s*) (\|) (\s*)/","/(\s*) (!) (\s*)/"); $new 1 = Array ('" ' ", '" | "', '" "! '); echo $keyword 3= "\" ". Preg_replace ($old 1, $new 1, $keyword)." \ "";//output: "Shanghai" | " Beijing "" "Chengdu" "Shenzhen" echo $keyword = "Shanghai | Beijing, Chengdu, Shenzhen "echo"
"; $old 1 = Array ("/(\s*) (\s) (\s*)/", "/(\s*) (\|) (\s*)/","/(\s*) (!) (\s*)/"); $new 1 = Array ('" ' ", '" | "', '" "! '); echo $keyword 3= "\" ". Preg_replace ($old 1, $new 1, $keyword)." \ "";//output: "Shanghai" "" | "" "Beijing" "" "! Chengdu" "Shenzhen"

The above two cases: the first is the display is right, the second to the symbol between the added space to lose the wrong. How do I block out the extra spaces?


Reply to discussion (solution)

Not understand your needs, I do the words with \s extraction is enough

Does the following approach satisfy your needs?

echo $keyword = "Shanghai | Beijing, Chengdu, Shenzhen"; echo "
"; $old 1 = Array ("/(\s*) (\s) (\s*)/", "/(\s*) (\|) (\s*)/","/(\s*) (!) (\s*)/"); $new 1 = Array ('" ' ", '" | "', '" "! '); echo $keyword 3= "\" ". Preg_replace ($old 1, $new 1, $keyword)." \ "";//output: "Shanghai" | " Beijing "" "Chengdu" "Shenzhen" echo $keyword = "Shanghai | Beijing, Chengdu, shenzhen "; $keyword = Str_replace (" "," ", $keyword); echo"
"; $old 1 = Array ("/(\s*) (\s) (\s*)/", "/(\s*) (\|) (\s*)/","/(\s*) (!) (\s*)/"); $new 1 = Array ('" ' ", '" | "', '" "! '); echo $keyword 3= "\" ". Preg_replace ($old 1, $new 1, $keyword)." \ "";//output: "Shanghai" "" | "" "Beijing" "" "! Chengdu" "Shenzhen"

Does the following approach satisfy your needs?



PHP Code
?



123456789101112131415

echo $keyword = "Shanghai | Beijing, Chengdu, Shenzhen"; echo "
"; $old 1 = Array ("/(\s*) (\s) (\s*)/", "/(\s*) (\|) (\s*)/","/(\s*) (!) (\s*)/"); $new 1 = Array (' ...
It's still not working. Your display is: "Shanghai" | " Beijing "", Chengdu, Shenzhen "
The result I want is: "Shanghai" | " BEIJING ""! "Chengdu" "Shenzhen"

Not understand your needs, I do the words with \s extraction is enough
My demand is: "Shanghai | Beijing, Chengdu, Shenzhen" into "Shanghai" | " BEIJING ""! "Chengdu" "Shenzhen"

Not understand your needs, I do the words with \s extraction is enough

"Shanghai | BEIJING, Chengdu, Shenzhen "also to become" Shanghai "|" BEIJING ""! "Chengdu" "Shenzhen"

Write like this

$keyword = "Shanghai | Beijing, Chengdu, Shenzhen";     Output: "Shanghai" | " Beijing "" "Chengdu" "shenzhen" $keyword = "Shanghai | Beijing, Chengdu,   Shenzhen ";//output:" Shanghai "|" BEIJING ""! "Chengdu" "Shenzhen" echo "
"; $old 1 = Array ("/!? [^\s!|] +/","/\s+/","/\s*\|\s*/","/""/"); $new 1 = Array ('" "$" ', ' "', ' | ', '" '), echo $keyword 3 = preg_replace ($old 1, $new 1, $keyw ORD);

Please refer to

$old 1 = Array ("/(\s*) (\|) (\s*)/","/(\s*) (!) (\s*)/","/(\s*) (\s) (\s*) (?! \ ")/"), $new 1 = Array (' "|" ', ' ""! ');

echo $keyword = "Shanghai | Beijing, Chengdu,   shenzhen "; $keyword = Str_replace (" "," ", $keyword); echo"
"; $old 1 = Array ("/(\s*) (\|) (\s*)/","/(\s*) (!) (\s*)/","/(\s*?) (\s) (\s*?) /"); $new 1 = Array ('" | "', '" "" "," "" "); echo $keyword 3=" \ "". Preg_replace ($old 1, $new 1, $keyword). " \ "";//output: "Shanghai" "" | "" "Beijing" "" "! Chengdu" "Shenzhen"

Give it a try
  • 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.