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