3v4l.org

run code in 300+ PHP versions simultaneously
<?php function highlight(string $search, string $number): string { $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>' . highlight($search, substr($number, $numberIndex + 1)); } } elseif (!in_array($character, [',', '.'])) { $searchBegin = NULL; $searchIndex = 0; } } return $number; } echo highlight('1234', '12,341,234.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
<span style="background: yellow">12,34</span><span style="background: yellow">1,234</span>.00

preferences:
59.41 ms | 406 KiB | 5 Q