PHP Code to Implement Fast Pattern Matching

pattern matching in php
Pattern matching is to check for the presence of some string in a given text. You must be wondering what I am intending to convey by the term fast in the title of this article.

Actually, PHP is already well enriched with many string manipulation functions including preg_match at apex , all contributing good for pattern matching. But they all seem to be very slow and with low performance when need to scale big, the pattern matching.

Implement Fast Pattern Matching Using PHP

Like if one needs to search for multiple words or patterns simultaneously in a text, existing PHP functions fail to provide desired performance.

PHP has added a wonderful package under its stock with name

“PHP fast pattern matching”

The full code can be downloaded from here:

The main file inside this package is fastpm.php which contains the definition of the main class facilitating the fast pattern matching php.


and here is the example using this wonderful class.


Here is the Output:

computer, pattern, match, of, of, of, of, pattern, pattern, match, pattern, of

The Output Reveals Some Interesting Points Here:

  • Output displays all the search patterns if present, with each pattern displayed exactly the number of times, it is present in the given text.
  • The output exactly follows the order in which the pattern is present in the given text.

Now have a try with this package for Fast Pattern Matching in PHP and tell me isn’t it fast?

Hopefully you would like this. Thanks

Leave a Comment