加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php – 将字符串分解为令牌,保持引用的substr完整

发布时间:2020-05-26 02:20:03 所属栏目:PHP 来源:互联网
导读:我不知道我在哪里看到它,但任何人都可以告诉我如何使用 PHP和正则表达式完成这个? this is a string that has quoted text inside. 我希望能够像这样爆炸它 [0]this[1]is[2]a[3]string[4]that has quoted text[5]inside 保持报价完整. 你可以试试下面的代码

我不知道我在哪里看到它,但任何人都可以告诉我如何使用 PHP和正则表达式完成这个?

'this is a string "that has quoted text" inside.'

我希望能够像这样爆炸它

[0]this
[1]is
[2]a
[3]string
[4]"that has quoted text"
[5]inside

保持报价完整.

你可以试试下面的代码:
$str = 'this is a string  "that has quoted text" inside.';
var_dump ( preg_split('#s*("[^"]*")s*|s+#',$str,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) );

Output: 
array(6) {
  [0]=>
  string(4) "this"
  [1]=>
  string(2) "is"
  [2]=>
  string(1) "a"
  [3]=>
  string(6) "string"
  [4]=>
  string(22) ""that has quoted text""
  [5]=>
  string(7) "inside."
}

这是above working code on dialpad的链接

更新:如需转发支持,请尝试:

preg_split('#s*((?<!\)"[^"]*")s*|s+#',PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读