php explode at capital letters?

前端 未结 4 1903
孤城傲影
孤城傲影 2020-12-28 12:23

I have strings like:

$a = \'helloMister\';
$b = \'doggyWaltz\';
$c = \'bumWipe\';
$d = \'pinkNips\';

How can I explode at the capital lette

相关标签:
4条回答
  • 2020-12-28 12:45

    Look up preg_split

    $result = preg_replace("([A-Z])", " $0", "helloMister");
    print_r(explode(' ', $result));
    

    hacky hack. Just don't have spaces in your input string.

    0 讨论(0)
  • 2020-12-28 12:53

    Extending Mathew's Answer, this works perfectly,

    $arr = preg_replace("([A-Z])", " $0", $str);
    $arr = explode(" ",trim($arr));
    
    0 讨论(0)
  • 2020-12-28 12:57

    If you want to split helloMister into hello and Mister you can use preg_split to split the string at a point just before the uppercase letter by using positive lookahead assertion:

    $pieces = preg_split('/(?=[A-Z])/',$str);
    

    and if you want to split it as hello and ister you can do:

    $pieces = preg_split('/[A-Z]/',$str);
    
    0 讨论(0)
  • 2020-12-28 12:57

    Look at preg_split, it's like explode but takes a regular expression

    preg_split('~[A-Z]~',$inputString)
    
    0 讨论(0)
提交回复
热议问题