Home >Backend Development >PHP Tutorial >根据大写字母分隔字符串

根据大写字母分隔字符串

WBOY
WBOYOriginal
2016-06-06 20:33:041387browse

比如

<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>

结果:

<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></code>
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