PHP explode the string, but treat words in quotes as a single word

You could use a preg_match_all(…): $text=”Lorem ipsum “dolor sit amet” consectetur “adipiscing \\”elit” dolor”; preg_match_all(“https://stackoverflow.com/”(?:\\\\.|[^\\\\”])*”|\S+/’, $text, $matches); print_r($matches); which will produce: Array ( [0] => Array ( [0] => Lorem [1] => ipsum [2] => “dolor sit amet” [3] => consectetur [4] => “adipiscing \”elit” [5] => dolor ) ) And as you can see, … Read more

Convert backslash-delimited string into an associative array

Using a simple regex via preg_match_all and array_combine is often the shortest and quickest option: preg_match_all(“/([^\\\\]+)\\\\([^\\\\]+)/”, $string, $p); $array = array_combine($p[1], $p[2]); Now this is of course a special case. Both keys and values are separated by a \ backslash, as are all pairs of them. The regex is also a bit lengthier due to … Read more