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

根据大写字母分隔字符串

WBOY
Release: 2016-06-06 20:33:04
Original
1372 people have browsed it

比如

<code>wordWordWordWord
</code>
Copy after login
Copy after login

分隔成数组

<code>word,Word,Word,Word
</code>
Copy after login
Copy after login

正则该怎么写

回复内容:

比如

<code>wordWordWordWord
</code>
Copy after login
Copy after login

分隔成数组

<code>word,Word,Word,Word
</code>
Copy after login
Copy after login

正则该怎么写

<code><?php $a = 'wordWordWordWordYelloHelloTest';

var_dump(preg_split('/(?=[A-Z])/', $a));

</code></code>
Copy after login

结果:

<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>
Copy after login

楼上的正则有够复杂,来个简单的,先替换再切割:

<code><?php $str = "wordWordWordWord";
$splitChar = ",";
$formatStr = preg_replace("/([A-Z])/", ",\\1", $str);

$wordList = explode($splitChar, $formatStr);
print_r($wordList);
</code></code>
Copy after login
Related labels:
php
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