Home > Backend Development > PHP Tutorial > How to Preserve Quoted Text as Single Words When Using PHP's explode()?

How to Preserve Quoted Text as Single Words When Using PHP's explode()?

Mary-Kate Olsen
Release: 2024-12-07 13:01:12
Original
432 people have browsed it

How to Preserve Quoted Text as Single Words When Using PHP's explode()?

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
Copy after login

Expected Output:

["Lorem", "ipsum", "dolor sit amet", "consectetur", "adipiscing elit", "dolor"]
Copy after login

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);
Copy after login

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);
Copy after login

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
Copy after login

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);
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template