I am searching for a function in PHP to return the array of position of a character in a string.
Inputing those parameters "hello world", 'o' would return (4,7).
Thanks in advance.
No looping needed
$str = 'Hello World';
$letter='o';
$letterPositions = array_keys(array_intersect(str_split($str),array($letter)));
var_dump($letterPositions);
you can check this http://www.php.net/manual/en/function.strpos.php#92849 or http://www.php.net/manual/en/function.strpos.php#87061, there are custom strpos functions to find all occurrences
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With