matecat/whole-text-finder
最新稳定版本:v1.0.16
Composer 安装命令:
composer require matecat/whole-text-finder
包简介
A simple whole text finder written in PHP
README 文档
README
WholeTextFinder is a simple whole text finder.
Basic Usage
Use the find method. Here is a basic search:
//.. use Finder\WholeTextFinder; $haystack = "PHP PHP is the #1 web scripting PHP language of choice."; $needle = "php"; // 3 matches $matches = WholeTextFinder::find($haystack, $needle); // $matches is equals to: // // array(3) { // [0] => // array(2) { // [0] => // string(3) "PHP" // [1] => // int(0) // } // [1] => // array(2) { // [0] => // string(3) "PHP" // [1] => // int(4) // } // [2] => // array(2) { // [0] => // string(3) "PHP" // [1] => // int(32) // } // }
Multi bytes strings
Please note that WholeTextFinder::find function is multi byte safe and returns the correct word positions in the original phrase. Take a look here:
//.. use Finder\WholeTextFinder; $haystack = "La casa è bella bella"; $needle = "bella"; $matches = WholeTextFinder::find($haystack, $needle, true, true, true); // $matches is equals to: // array ( // 0 => // array ( // 0 => 'bella', // 1 => 10, // ), // 1 => // array ( // 0 => 'bella', // 1 => 16, // ), //)
Find and Replace
There is also available a findAndReplace method:
//.. use Finder\WholeTextFinder; $haystack = 'Δύο παράγοντες καθόρισαν την αντίληψή μου για την Τενεσί Ουίλιαμς και τη σκηνική παρουσίαση των κειμένων: η Maria Britneva και η Annette Saddik, αφετέρου.'; $needle = 'και'; $replacement = 'test'; $matches = WholeTextFinder::findAndReplace($haystack, $needle, $replacement); // $matches is equals to: // // array(2) { // ["replacement"]=> // string(252) "Δύο παράγοντες καθόρισαν την αντίληψή μου για την Τενεσί Ουίλιαμς test τη σκηνική παρουσίαση των κειμένων: η Maria Britneva test η Annette Saddik, αφετέρου." // ["occurrencies"]=> // array(2) { // [0]=> // array(2) { // [0]=> // string(6) "και" // [1]=> // int(66) // } // [1]=> // array(2) { // [0]=> // string(6) "και" // [1]=> // int(123) // } // } // } //
This method will automatically exclude from replace HTML and some Matecat special tags, but allows to replace the inner content inside HTML tags.
So, for example:
//.. use Finder\WholeTextFinder; // Example 1 $haystack = "Beauty -> 2 Anti-Akne Gesichtsreiniger Schlankmacher <g id=\"2\">XXX</g>"; $needle = 2; $replacement = "test"; $matches = WholeTextFinder::findAndReplace($haystack, $needle, $replacement); // $matches is equals to: // // array(2) { // ["replacement"]=> // string(252) "Beauty -> test Anti-Akne Gesichtsreiniger Schlankmacher <g id="2">XXX</g>" // ["occurrencies"]=> // array(1) { // [0]=> // array(2) { // [0]=> // string(1) "2" // [1]=> // int(10) // } // } // } // // Example 2 $haystack = "Beauty -> 2 Anti-Akne Gesichtsreiniger Schlankmacher <g id=\"2\">XXX</g>"; $needle = 'XXX'; $replacement = "test"; $matches = WholeTextFinder::findAndReplace($haystack, $needle, $replacement); // $matches is equals to: // // array(2) { // ["replacement"]=> // string(252) "Beauty -> 2 Anti-Akne Gesichtsreiniger Schlankmacher <g id="2">test</g>" // ["occurrencies"]=> // array(1) { // [0]=> // array(2) { // [0]=> // string(1) "test" // [1]=> // int(55) // } // } // } //
Search options
Some options are avaliable:
You can also specify four options:
- $skipHtmlEntities (
trueby default) - $exactMatch (
falseby default) - $caseSensitive (
falseby default) - $preserveNbsps (
falseby default)
Here are some examples:
//.. use Finder\WholeTextFinder; $haystack = "PHP PHP is the #1 web scripting PHP language of choice."; // 0 matches $needle = "php"; $matches = WholeTextFinder::find($haystack, $needle, true, true, true); // 1 match $needle = "#1"; $matches = WholeTextFinder::find($haystack, $needle, true, true, true); // 1 match, even if the haystack contains an invisible nbsp and the needle has an ordinary spacer $haystackWithNbsp = "Lawful basis for processing including basis of legitimate interest"; $needleWithoutNbsp = "Lawful basis for processing including basis of legitimate interest"; $matches = WholeTextFinder::find($haystackWithNbsp, $needleWithoutNbsp, true, true, true);
Support
If you found an issue or had an idea please refer to this section.
Authors
- Mauro Cassani - github
License
This project is licensed under the MIT License - see the LICENSE.md file for details
统计信息
- 总下载量: 12.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-03