1. Go to this page and download the library: Download loupe/matcher library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
loupe / matcher example snippets
use Loupe\Matcher\Tokenizer\Tokenizer;
use Loupe\Matcher\Matcher;
use Loupe\Matcher\Formatter;
use Loupe\Matcher\FormatterOptions;
$tokenizer = new Tokenizer();
$matcher = new Matcher($tokenizer);
$formatter = new Formatter($matcher);
$options = (new FormatterOptions())
->withEnableHighlight()
->withEnableCrop()
->withCropLength(10);
$result = $formatter->format(
'This is a long document with many words to search through and compare.',
'search words',
$options
);
// "...with many <em>words</em> to <em>search</em> through..."
echo $result->getFormattedText();
$tokenizer = new Tokenizer('en_US'); // Optional locale
$tokens = $tokenizer->tokenize('search for "exact phrase" -exclude');
$tokens->all(); // All tokens
$tokens->phraseGroups(); // Quoted phrases only
$tokens->allNegated(); // Terms to exclude
$matcher = new Matcher($tokenizer, ['the', 'and', 'or']); // Stop words
$matches = $matcher->calculateMatches('Text to search', 'search query');
// Get position information for highlighting
$spans = $matcher->calculateMatchSpans('Text to search', 'query', $matches);
foreach ($spans as $span) {
echo "Match at position {$span->getStartPosition()}-{$span->getEndPosition()}";
}
class CustomTokenizer implements TokenizerInterface {
public function tokenize(string $text): TokenCollection {
// Your custom tokenization logic
}
public function matches(Token $token, TokenCollection $tokens): bool {
// Your custom logic for checking if a token is a match
}
}
$cropper = new \Loupe\Matcher\Formatting\Cropper(
cropLength: 50,
cropMarker: '…',
highlightStartTag: '<em>',
highlightEndTag: '</em>'
);
// "...text with <em>highlighted</em> terms."
echo $cropper->cropHighlightedText('Long text with <em>highlighted</em> terms.');
// Assume you already have matches from somewhere else
$existingMatches = new TokenCollection(/* ... */);
// Set up the tokenizer, matcher, and formatter as usual
$tokenizer = new Tokenizer();
$matcher = new Matcher($tokenizer);
$formatter = new Formatter($matcher);
$options = (new FormatterOptions())
->withEnableHighlight()
->withEnableCrop()
->withCropLength(100);
// Format using the existing matches - no duplicate processing
$result = $formatter->format($text, $query, $options, matches: $existingMatches);
echo $result->getFormattedText();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.