比如
<code>wordWordWordWord </code>
分隔成数组
<code>word,Word,Word,Word </code>
正则该怎么写
比如
<code>wordWordWordWord </code>
分隔成数组
<code>word,Word,Word,Word </code>
正则该怎么写
<code><?php $a = 'wordWordWordWordYelloHelloTest'; var_dump(preg_split('/(?=[A-Z])/', $a)); </code>
结果:
<code>array(7) { [0]=> string(4) "word" [1]=> string(4) "Word" [2]=> string(4) "Word" [3]=> string(4) "Word" [4]=> string(5) "Yello" [5]=> string(5) "Hello" [6]=> string(4) "Test" } </code>
楼上的正则有够复杂,来个简单的,先替换再切割:
<code><?php $str = "wordWordWordWord"; $splitChar = ","; $formatStr = preg_replace("/([A-Z])/", ",\\1", $str); $wordList = explode($splitChar, $formatStr); print_r($wordList); </code>