PHP code example of codeinc / query-tokens-extractor
1. Go to this page and download the library: Download codeinc/query-tokens-extractor 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/ */
codeinc / query-tokens-extractor example snippets
use CodeInc\QueryTokenExtractor\QueryTokenExtractor;
use CodeInc\QueryTokensExtractor\Type\RegexType;
use CodeInc\QueryTokensExtractor\Type\WordType;
use CodeInc\QueryTokensExtractor\Type\FrenchPhoneNumberType;
use CodeInc\QueryTokensExtractor\Type\FrenchPostalCodeType;
use CodeInc\QueryTokensExtractor\Type\YearType;
use CodeInc\QueryTokensExtractor\Dto\QueryToken;
$tokensExtractor = new QueryTokensExtractor([
new FrenchPhoneNumberType(),
new FrenchPostalCodeType(),
new YearType(),
new RegexType('my_custom_token', '/^this a custom token/ui'),
new WordType(),
]);
$tokens = $tokensExtractor->extract('paris (75001) these are words 01.00.00.00.00 this a custom token 2023');
/** @var QueryToken $token */
foreach ($tokens as $token) {
echo "Position: " . $token->position . "\n"
."Class: " . get_class($token->type) . "\n"
."Name: " . $token->type->name . "\n"
."Value: " . $token->value . "\n";
}
use CodeInc\QueryTokenExtractor\QueryTokenExtractor;
use CodeInc\QueryTokensExtractor\Type\WordType;
use CodeInc\QueryTokensExtractor\Type\FrenchPhoneNumberType;
use CodeInc\QueryTokensExtractor\Type\FrenchPostalCodeType;
use CodeInc\QueryTokensExtractor\Type\YearType;
$tokensExtractor = new QueryTokensExtractor([
new FrenchPhoneNumberType(), // highest priority
new FrenchPostalCodeType(),
new YearType(),
new WordType(), // lowest priority
]);
use CodeInc\QueryTokensExtractor\Type\RegexType;
class MyCustomTokenType extends RegexType
{
public function __construct()
{
parent::__construct(
name: 'my_custom_token',
regexp: '/^this a custom token/ui'
);
}
}
// alternatively tokens can be defined directly using the RegexType class
$myCustomToken2 = new RegexType(
name: 'my_custom_token',
regexp: '/^this a custom token/ui'
);
use CodeInc\QueryTokensExtractor\Type\RegexType;
$tokensExtractor = new QueryTokensExtractor([
new RegexType(
name: 'my_custom_token',
regexp: '/^this a custom token/ui',
// a simple closure called by QueryToken::getFormattedValue()
valueFormatter: fn($value) => strtoupper($value)
)
]);
$tokens = $tokensExtractor->extract('this a custom token');
$tokens->getByPosition(0)->getFormattedValue(); // THIS A CUSTOM TOKEN
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.