<?php
function highlight($needle, $haystack, $respect, $highlightStart, $highlightEnd) {
$replacer = static function ($matches) use ($respect, $highlightStart, $highlightEnd) {
return
$highlightStart .
preg_replace('([^' . $respect . ']+)', $highlightEnd . '$0' . $highlightStart, $matches[0]) .
$highlightEnd;
};
$needle = str_split($needle);
foreach ($needle as &$l) {
$l = preg_quote($l);
}
unset($l);
$needle = implode('[^' . $respect . ']*', $needle);
return preg_replace_callback('(' . $needle . ')', $replacer, $haystack);
}
echo highlight('1234', '(123) 456-7890', '0-9', '<b>', '</b>');
preferences:
39.99 ms | 402 KiB | 5 Q