Skip to content

Commit

Permalink
Allow to set the limit number of replacements when highlighting text
Browse files Browse the repository at this point in the history
  • Loading branch information
macnie authored and chinpei215 committed Mar 7, 2017
1 parent b7f4487 commit 242cd2a
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Utility/Text.php
Expand Up @@ -473,7 +473,8 @@ public static function highlight($text, $phrase, array $options = [])
$defaults = [
'format' => '<span class="highlight">\1</span>',
'html' => false,
'regex' => "|%s|iu"
'regex' => "|%s|iu",
'limit' => -1,
];
$options += $defaults;
extract($options);
Expand All @@ -492,15 +493,15 @@ public static function highlight($text, $phrase, array $options = [])
$replace[] = sprintf($options['regex'], $segment);
}

return preg_replace($replace, $with, $text);
return preg_replace($replace, $with, $text, $limit);
}

$phrase = '(' . preg_quote($phrase, '|') . ')';
if ($html) {
$phrase = "(?![^<]+>)$phrase(?![^<]+>)";
}

return preg_replace(sprintf($options['regex'], $phrase), $format, $text);
return preg_replace(sprintf($options['regex'], $phrase), $format, $text, $limit);
}

/**
Expand Down

0 comments on commit 242cd2a

Please sign in to comment.