Search code examples
phpsplittext-extractionoverlapping-matches

Create an array of each character and its following character from an input string


I have a string

$str = "Diwas"

I want to make an array of every two alphabet so I used

str_split($str, 2);

So my array will be now

Di
wa
s

But I want to make an array in such a way that the result will be.

Di
iw
wa
as
s

Solution

  • This one is more ressource friendly than a preg_*

    $str="Diwas"
    
    $myArray = array();
    
    for ($i = 0; $i < strlen($str); ++$i)
        $myArray[] = substr($str, $i, 2);