我试图在大海捞针中找到所有针的位置:
$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);
的值在$pos
这里:
Array ( [0] => one [4] => two )
然而,预期是:
Array ( [0] => one [9] => two [13] => one)
所以有两件事出错了:
twoo
被标记为发生two
- 循环显然与第二次出现不匹配
one
我究竟做错了什么?