Home  >  Article  >  Backend Development  >  正则瓜分字符

正则瓜分字符

WBOY
WBOYOriginal
2016-06-13 13:06:53880browse

正则分割字符
字符串是
ul[class=xl xl2 cl] div.test ul[id=test]

我想用空格分割字符,但是[]号里面的空格不要分,单用正则可以实现这个功能吗?比如分之后,结果如下
ul[class=xl xl2 cl]
div.test 
ul[id=test]

如果正则无法实现,其他思路呢?

------解决方案--------------------

PHP code
$s = 'ul[class=xl xl2 cl] div.test sasa ul[id=test]';
preg_match_all('/\w+\[[^]]+]|[^\s]+/', $s, $r);
print_r($r); <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn