https://github.com/matecat/whole-text-finder
Whole Text Searcher
https://github.com/matecat/whole-text-finder
Last synced: 5 months ago
JSON representation
Whole Text Searcher
- Host: GitHub
- URL: https://github.com/matecat/whole-text-finder
- Owner: matecat
- License: mit
- Created: 2019-08-29T16:36:57.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2025-02-14T11:28:01.000Z (over 1 year ago)
- Last Synced: 2025-10-28T22:02:52.798Z (7 months ago)
- Language: PHP
- Size: 3.13 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# WholeTextFinder
[]()
[]()
[](https://scrutinizer-ci.com/g/matecat/whole-text-finder/?branch=master)
**WholeTextFinder** is a simple whole text finder.
## Basic Usage
Use the `find` method. Here is a basic search:
```php
//..
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:
```php
//..
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:
```php
//..
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:
```php
//..
use Finder\WholeTextFinder;
// Example 1
$haystack = "Beauty -> 2 Anti-Akne Gesichtsreiniger Schlankmacher XXX";
$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 XXX"
// ["occurrencies"]=>
// array(1) {
// [0]=>
// array(2) {
// [0]=>
// string(1) "2"
// [1]=>
// int(10)
// }
// }
// }
//
// Example 2
$haystack = "Beauty -> 2 Anti-Akne Gesichtsreiniger Schlankmacher XXX";
$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 test"
// ["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** (`true` by default)
* **$exactMatch** (`false` by default)
* **$caseSensitive** (`false` by default)
* **$preserveNbsps** (`false` by default)
Here are some examples:
```php
//..
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](https://github.com/mauretto78/whole-text-finder/issues).
## Authors
* **Mauro Cassani** - [github](https://github.com/mauretto78)
## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details