I’m trying to find the position of all needles in a haystack:
$haystack = 'one twoo two one postpone twool';
$needles = array('one', 'two', 'three');
foreach ($needles as $needle) {
if (stristr($haystack, $needle)) { // list position of all needles
$pos[strpos($haystack, $needle)] = $needle;
}
}
print_r($pos);
The value of $pos is here:
Array ( [0] => one [4] => two )
However, expected was:
Array ( [0] => one [9] => two [13] => one)
So two things go wrong:
twoois marked as being an occurence oftwo- the loop apparently doesn’t match the second occurence of
one
What am I doing wrong?
If you want an easy way, you could use
preg_match: