3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getAllWholeWordPos($s,$word){ $retval = false; $b = " "; for ($i=0, $max = strlen( $s ); $i < $max; $i++) { if ( !ctype_alpha( $s[$i] ) ){ $s[$i] = $b; } } while ( ( $pos = stripos( $s, $word) ) !== false ) { $retval[] = $pos; var_dump($word,$retval); for ( $i=0, $max = $pos + 1 + strlen( $word ); $i <= $max; $i++) { $s[$i] = $b; } } return [$retval,$s]; } $str = "TheraininSpainstaysmainlyintheplain."; $d = ["The", "rain", "in", "Spain","stays","mainly","plain"]; $arr = null; for ($i=0, $max = count($d); $i < $max; $i++) { list($r,$str) = getAllWholeWordPos($str,$d[$i]); if (is_array($r)) { foreach ($r as $key => $val) { $arr[$key] = $val; } } else { $arr[$r] = $d[$i]; } } var_dump($arr);
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Deprecated: Automatic conversion of false to array is deprecated in /in/fYdNN on line 14 string(3) "The" array(1) { [0]=> int(0) } string(3) "The" array(2) { [0]=> int(0) [1]=> int(27) } Deprecated: Automatic conversion of false to array is deprecated in /in/fYdNN on line 14 string(2) "in" array(1) { [0]=> int(33) } array(2) { [0]=> string(5) "plain" [1]=> int(27) }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Deprecated: Automatic conversion of false to array is deprecated in /in/fYdNN on line 14 string(3) "The" array(1) { [0]=> int(0) } string(3) "The" array(2) { [0]=> int(0) [1]=> int(27) } Deprecated: Automatic conversion of false to array is deprecated in /in/fYdNN on line 14 string(2) "in" array(1) { [0]=> int(33) } array(2) { [0]=> string(5) "plain" [1]=> int(27) }
Output for 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
string(3) "The" array(1) { [0]=> int(0) } string(3) "The" array(2) { [0]=> int(0) [1]=> int(27) } string(2) "in" array(1) { [0]=> int(33) } array(2) { [0]=> string(5) "plain" [1]=> int(27) }

preferences:
182.41 ms | 403 KiB | 217 Q