3v4l.org

run code in 300+ PHP versions simultaneously
<?php function highlight(string $search, string $number) { $searchBegin = NULL; $searchIndex = 0; $search = str_replace([',', '.'], '', $search); foreach (str_split($number) as $numberIndex => $character) { if ($character == $search[$searchIndex]) { if (is_null($searchBegin)) { $searchBegin = $numberIndex; } $searchIndex++; if ($searchIndex == strlen($search)) { return substr($number, 0, $searchBegin) . '<span style="background: yellow">' . substr($number, $searchBegin, $numberIndex - $searchBegin + 1) . '</span>' . substr($number, $numberIndex +1); } } elseif (!in_array($character, [',', '.'])) { $searchBegin = NULL; $searchIndex = 0; } } return $number; } echo highlight('4321', '12,345,678.00') . PHP_EOL; echo highlight('1234', '12,345,678.00') . PHP_EOL; echo highlight('4567', '12,345,678.00') . PHP_EOL; echo highlight('3,456', '12,345,678.00') . PHP_EOL; echo highlight('7.80', '12,345,678.00') . PHP_EOL;
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
12,345,678.00 <span style="background: yellow">12,34</span>5,678.00 12,3<span style="background: yellow">45,67</span>8.00 12,<span style="background: yellow">345,6</span>78.00 12,345,6<span style="background: yellow">78.0</span>0

preferences:
60.11 ms | 407 KiB | 5 Q