i need to find the all positions of a particular character in a string. I am using the following code
$pos = 0;
$positions = array();
while( $pos = strpos($haystack,$needle,$pos){
$positions[] = $pos;
$pos = $pos+1;
}
the problem with this code is that when the needle
is at location 1,its returns 1 and so doesn't enter the loop.
So i tried the following
$pos = 0;
$positions = array();
while( ($pos = strpos($haystack,$needle,$pos) || (strpos($haystack,$needle,$pos)=== 0){
$positions[] = $pos;
$pos = $pos+1;
}
and,
$pos = 0;
$positions = array();
while( ($pos = strpos($haystack,$needle,$pos) || (strpos($haystack,$needle,$pos) != false){
$positions[] = $pos;
$pos = $pos+1;
}
But nothing seems to be working. Is there any other way.
The two alternative i tried give me
Allowed memory size of 268435456 bytes exhausted
which i think has more to do with programming error than memory issue.
Plz help.