我如何将一个空格分隔的令牌字符串与转义字符分开?(How do I split a whitespace delimited string of tokens with an escape chara

编程入门 行业动态 更新时间:2024-10-26 07:30:48
我如何将一个空格分隔的令牌字符串与转义字符分开?(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 )

更多推荐

本文发布于:2023-07-28 01:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298230.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   空格   字符串   如何将   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!