How to explode a string using capital letters as a separator?

Hi, I am trying to achieve this using php explode, but I am having problems with this.

here is my code

<?php
$capitals = array("A","B","C");
$word_break = "brandonBjakeCsullano";

$count = 0;

while ($count <=2)
{

$break =  explode($capitals[$count++],$word_break)


}
echo $break[0];
echo "<br/>";
echo $break[1];

?>

using the code above, I can get this result:

brandonBjake

sullano

and this is what I'm trying to achieve

Brandon

Jake

sullano

Appreciate your help.

Any strategy is acceptable, not my style of implementation. thanks in advance

+4
source share
4 answers

There is always a good old preg_splitone that allows you to use a regular expression to split a string into an array.

Something like (untested):

$names = preg_split( '/[A-Z]/', 'brandonBjakeCsullano' );
print_r( $names );
+1
source

Try using str_replaceand explodehow

$capitals = array("A","B","C");
$word_break = "brandonBjakeCsullano";
$word_break = str_replace($capitals, '/',$word_break);
print_r(explode('/',$word_break));//Array ( [0] => brandon [1] => jake [2] => sullano )

Edited ,

echo implode('<br>',explode('/',$word_break));// instead of print_r

1) print_r(explode('/',$word_break));

OK (0.008 sec real, 0.008 sec wall, 14 MB, 71 syscalls)

2) echo implode("\n",explode('/',$word_break));

OK (0.008 sec real, 0.009 sec wall, 14 MB, 42 syscalls)

3) $res = explode ('/', $word_break); foreach ($ res $value) {   echo $value. "\n" ;
}

OK (0.012 sec real, 0.015 sec wall, 14 MB, 44 syscalls)
+3

Here is another example:

<?php
$capitals = array("A","B","C");
$string = "brandonBjakeCsullano";

$count = 0;
$capitals = array("A","B","C");
$string = "brandonBjakeCsullano";
$string = str_replace($capitals, '/',$string);
$res= explode('/',$string);
foreach($res as $value){ 
    echo $value."<br>";   
}
?>

Output: -

brandon
jake
sullano

For reference, click here.

+2
source

try the following:

<?php
$capitals = array("A","B","C");
$word_break = "brandonBjakeCsullano";
$arr=str_split($word_break);
for($i=0;$i <=strlen($word_break);$i++){
    for($j=0;$j<=2;$j++){
        if($arr[$i]!=$capitals[$j]){
            $flag=TRUE;
        }
        else{
            $flag=FALSE;
            echo '<br>';
            break;
        }

    }
    if($flag==TRUE){
        echo $arr[$i];
    }
}

?>
0
source

All Articles