php explode string with Uppercase if lowercase letter exists before it without space

元气小坏坏 提交于 2019-12-30 11:32:12

问题


$str="Hello MotoBell RingsKing Speech";

I need explode this string by uppercase letter if lowercase letter exists before it.

like this:

$splitted=array(
      0=>"Hello Moto",
      1=>"Bell Rings",
      2=>"King Speech"
     );

any ideas?

I try use that reg_ex, but not working:

$pieces = preg_split('/(?=[A-ZА-Я])/u', $str, -1, PREG_SPLIT_NO_EMPTY);

回答1:


var_dump(preg_split('/(?<=[a-z])(?=[A-Z])/', 'Hello MotoBell RingsKing Speech'))

// array(3) {
//   [0]=>
//   string(10) "Hello Moto"
//   [1]=>
//   string(10) "Bell Rings"
//   [2]=>
//   string(11) "King Speech"
// }


来源:https://stackoverflow.com/questions/24951067/php-explode-string-with-uppercase-if-lowercase-letter-exists-before-it-without-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!