用空格和括号将句子(HTML代码)分成单词



例如我想分割以下HTML代码:

<a href="http://www.google.com">Google</a>

输出应该用空格和尖括号分隔

Array(
[0] => <
[1] => a
[2] => href="http://www.google.com"
[4] => >
[5] => Google
[6] => <
[7] => /a
[8] => >

不确定您想要实现什么,但是对于您的示例,您可以使用选项PREG_SPLIT_DELIM_CAPTURE,它在结果中包含捕获的分隔符部分:

$result = preg_split('/([<>])| /', $txt, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
>>> '<a href="http://www.google.com">Google</a>'
    .match(/(<)(w+)s+(href="[w/:.]+")s*(>)(.*)?(<)(/w)(>)/)
 ["<a href="http://www.google.com">Google</a>" 
  ,"<", "a", "href="http://www.google.com"", ">", "Google", "<", "/a", ">"]

最新更新