3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getAllWholeWordPos($s,$word){ $b = " "; $delimited = "$b$word$b"; $retval = false; for ($i=0, $max = strlen( $s ); $i < $max; $i++) { if ( !ctype_alpha( $s[$i] ) ){ $s[$i] = $b; } } while ( ( $pos = stripos( $s, $delimited) ) !== false ) { $retval[] = $pos + 1; for ( $i=0, $max = $pos + 1 + strlen( $word ); $i <= $max; $i++) { $s[$i] = $b; } } return $retval; } $whole_word = "any"; $str = "Will *$whole_word* company do *$whole_word* job, (are there $whole_word)?"; echo "\nString: \"$str\""; $result = getAllWholeWordPos( $str, $whole_word ); $times = count( $result ); echo "\n\nThe word \"$whole_word\" occurs $times times:\n"; foreach ($result as $pos) { echo "\nPosition: ",$pos; }

preferences:
25.65 ms | 405 KiB | 5 Q