I need to tokenize a whitespace delimited string like hey 'this is' some text
into an array ['hey', 'this is', 'some', 'text']
(single quote character being the escape character).
What I have so far will split on whitespace, but it doesn't incorporate the necessary escape character.
$tokens = preg_split('/[\ \n\,]+/', $whitespaceDelimitedString);
Regular expression ninjas, come forth!! Please and thanks.
You can use this code:
$s = "hey 'this is' some text";
$a = preg_split("/'([^']*)'\s*|\s+/", $s, 0, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
print_r($a);
Array
(
[0] => hey
[1] => this is
[2] => some
[3] => text
)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments