How to a split string and get the start word index too by a regular expression in PHP

1 Answer

0 votes
$s = 'PHP programming language';
$arr = preg_split('/ /', $s, -1, PREG_SPLIT_OFFSET_CAPTURE);

echo "<pre>";
print_r($arr);
echo "</pre>";


/*
run:
    
Array
(
    [0] => Array
        (
            [0] => PHP
            [1] => 0
        )

    [1] => Array
        (
            [0] => programming
            [1] => 4
        )

    [2] => Array
        (
            [0] => language
            [1] => 16
        )

)

       
*/

 



answered Jul 15, 2016 by avibootz
...