yarri / keywords-highlighter
Highlights keywords (typically a search query) in a HTML string
Installs: 16 663
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.4.0
- ext-mbstring: *
Requires (Dev)
- atk14/tester: *
README
Highlights keywords (typically a search query) in a HTML string.
Basic usage
$html_text = '
<h1>The truth about pizza and beer</h1>
<p>
Beer and pizza.
It might be one of the most obvious food pairings on the planet...
</p>
';
$highlighter = new \Yarri\KeywordsHighlighter();
echo $highlighter->highlight($html_text,"pizza beer");
// <h1>The truth about <mark>pizza</mark> and <mark>beer</mark></h1>
//
// <p>
// <mark>Beer</mark> and <mark>pizza</mark>.
// It might be one of the most obvious food pairings on the planet...
// </p>
The default tag for highlighting can be overwritten in options of the constructor.
$highlighter = new \Yarri\KeywordsHighlighter([
"opening_tag" => '<span class="highlight">',
"closing_tag" => '</span>',
]);
echo $highlighter->highlight($html_text,"pizza beer");
// <h1>The truth about <span class="highlight">pizza</span> and <span class="highlight">beer</span></h1>
//
// <p>
// <span class="highlight">Beer</span> and <span class="highlight">pizza</span>.
// It might be one of the most obvious food pairings on the planet...
// </p>
KeywordsHighlighter doesn't highlight something inside a html tag.
$html_text = '
<h1>The truth about the H1 element</h1>
';
$highlighter = new \Yarri\KeywordsHighlighter();
echo $highlighter->highlight($html_text,"h1");
// <h1>The truth about the <mark>H1</mark> element</h1>
Installation
composer require yarri/keywords-highlighter
Testing
composer update --dev
cd test
../vendor/bin/run_unit_tests