Treating Quoted Words as Single Words in PHP explode
How can we break down a string, as seen below, using explode() while preserving quoted text as single words?
Lorem ipsum "dolor sit amet" consectetur "adipiscing elit" dolor
Expected Output:
["Lorem", "ipsum", "dolor sit amet", "consectetur", "adipiscing elit", "dolor"]
Despite attempts using the following code, the current implementation segments each word in the string:
$mytext = "Lorem ipsum %22dolor sit amet%22 consectetur %22adipiscing elit%22 dolor" $noquotes = str_replace("%22", "", $mytext"); $newarray = explode(" ", $noquotes);
Solution:
To accurately capture quoted words as single entities, we can leverage preg_match_all():
$text = 'Lorem ipsum "dolor sit amet" consectetur "adipiscing \"elit" dolor'; preg_match_all('/"(?:\\.|[^\\"])*"|\S+/', $text, $matches); print_r($matches);
This refined syntax handles even escaped quotes within quoted phrases.
Breakdown of Regex:
"(?: # Start non-capture group 1 \ # Match backslash character . # Match any character except line breaks | # Or [^\"] # Match any character except backslash and double quotes )* # End non-capture group 1 and repeat it zero or more times " # Match double quotes | # Or \S+ # Match one or more non-whitespace characters
Handling " Instead of Double Quotes:
Should the need arise to deal with " in place of double quotes, utilize this adjusted regex:
preg_match_all('/%22(?:\\.|(?!%22).)*%22|\S+/', $text, $matches);
The above is the detailed content of How to Preserve Quoted Text as Single Words When Using PHP's explode()?. For more information, please follow other related articles on the PHP Chinese website!