【问题标题】:php explode string with Uppercase if lowercase letter exists before it without space如果小写字母在它之前没有空格,则php用大写分解字符串
【发布时间】:2014-07-25 08:14:35
【问题描述】:
$str="Hello MotoBell RingsKing Speech";

如果前面有小写字母,我需要用大写字母分解这个字符串。

像这样:

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

有什么想法吗?

我尝试使用该 reg_ex,但不起作用:

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

【问题讨论】:

  • 使用正则表达式,卢克!
  • 你能举个例子吗?

标签: php split explode


【解决方案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"
// }

【讨论】:

  • 如果您想了解模式,请阅读任何有关前瞻/后瞻正则表达式的手册。
猜你喜欢
  • 1970-01-01
  • 2017-03-20
  • 2018-05-10
  • 2011-06-16
  • 2019-05-13
  • 2011-10-18
  • 2018-12-29
  • 2018-12-13
  • 2019-06-08
相关资源
最近更新 更多