我如何将一个空格分隔的令牌字符串与转义字符分开?(How do I split a whitespace delimited string of tokens with an escape character?)
我需要标记一个空格分隔的字符串,就像hey 'this is' some text到数组['hey', 'this is', 'some', 'text'] (单引号字符是转义字符)。
我到目前为止将会分割空白,但它没有包含必要的转义字符。
$tokens = preg_split('/[\ \n\,]+/', $whitespaceDelimitedString);正则表达忍者,出来! 请和谢谢。
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.
最满意答案
你可以使用这个代码:
$s = "hey 'this is' some text"; $a = preg_split("/'([^']*)'\s*|\s+/", $s, 0, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); print_r($a);OUTPUT:
Array ( [0] => hey [1] => this is [2] => some [3] => text )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);OUTPUT:
Array ( [0] => hey [1] => this is [2] => some [3] => text )更多推荐
发布评论