Home > Backend Development > PHP Tutorial > 初学新手问个正则的问题

初学新手问个正则的问题

WBOY
Release: 2016-06-23 14:04:40
Original
814 people have browsed it

研究好半天也没弄对

将 
/aaa/bbb/ccc

拆分成 
/aaa
/bbb
/ccc

我写的不对啊
preg_match('/(\/\w+)+/i', '/aaa/bbb/ccc', $matches);


回复讨论(解决方案)

一定要用正则吗,我觉得用explode函数也不错呀。

$str = '/aaa/bbb/ccc';$matches = explode('/',$str); //explodeecho '/'.$matches[1];  // /aaaecho '/'.$matches[2];  // /bbbecho '/'.$matches[3];  // /cccpreg_match_all('@\/[a-z]{3}@i', $str, $matches); //正则echo $matches[0][0];  // /aaaecho $matches[0][1];  // /bbbecho $matches[0][2];  // /ccc
Copy after login


要这样写
preg_match_all('/(\/\w+)/i', '/aaa/bbb/ccc', $matches);

$matches[1] 即是

3楼正解,是要匹配所有符合要求的,因此要用函数preg_match_all。

个人觉得没有必要去用正则,explode()完全可以做到你想要的结果,何必要舍近求远呢,当然也得看实际情况....

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template