RoiEX RoiEX - 1 month ago 5
PHP Question

Split string into doublequoted and non doublequoted sections



I'm currently working on a PHP project with a search engine which requires me to break down a search query to an array of substrings.

I want to support "doublequoting" so that the user is able to search for exact results e.g. "super secret stuff" only gets result with that exact string in their name while the same string without quotes returns results for "super" or "secret" or "stuff".

These quotes are supposed to be escapable.

Let me give you an example:


keyword Monday "Larrys \"House\"" epic


Should return an array of

[keyword, Monday, Larrys "House", epic]


I would like to support escaping of the backslashes too if this isn't too difficult to implement e.g.
\\"
still counts as a quote, while
\\\"
doesn't and so on.


Any help is appreciated!

Answer

There is no need to use regex for this as php provides very good CSV parsing routines. Using str_getcsv:

$input = 'keyword Monday "Larrys \"House\"" epic';

$arr = str_getcsv($input, " ");

// remove \\ from array
$arr = array_map(function($elem) { return str_replace('\\', '', $elem); }, $arr);

// print resulting array
print_r($arr);

Code Demo

Output:

Array
(
    [0] => keyword
    [1] => Monday
    [2] => Larrys "House"
    [3] => epic
)
Comments