3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Напишите реализацию для ф-ии strstr: * function strstr(string $haystack, string $needle) * нельзя использовать встроенные ф-ии php: substr, strtok, ... * (*) это более удачная реализация, работает за О(n) */ /** * Find substring starting in string, starting from some symbol(s) * @param string $haystack * @param string $needle * @return string */ function my_strstr2(string $haystack, string $needle): string { $isFound = false; $lenHaystack = strlen($haystack); $lenNeedle = strlen($needle); for($i = 0, $j = 0, $cnt = 0; $i < $lenHaystack, $j < $lenNeedle; $i++) { if($haystack[$i + $j] == $needle[$j]) { $j++; } else { $j = 0; } if($j >= strlen($lenNeedle)) { $isFound = true; break; } } return ($isFound ? substr($haystack, $i) : ''); } var_dump(my_strstr2('asdfghend', 'fg')); // 'fghend';
Output for git.master_jit, git.master, rfc.property-hooks
string(6) "fghend"

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
25.94 ms | 405 KiB | 5 Q