PHP Classes

How Can PHP Search String in Text Using the Package RA KMP Search: Search text strings using the KMP algorithm

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-05-02 (4 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
ra-kmp-search 1.0.0GNU General Publi...7Algorithms, Searching, Text processing, P...
Description 

Author

This package can search text strings using the Knuth-Morris-Pratt algorithm.

The Knuth-Morris-Pratt (KMP) algorithm is an efficient pattern search method that locates occurrences of a substring (pattern) within a longer string (text).

Unlike brute-force search that can go back in text, KMP avoids redundant comparisons by using a pre-computed table.

It provides a class with a function that can take as parameters a given text string and a pattern to search that string.

The class returns an array with the positions of the text pattern.

Picture of Roberto Aleman
  Performance   Level  
Innovation award
Innovation award
Nominee: 17x

Winner: 2x

 

Instructions

Please read the README.md document to learn how to perform text string searches using a pattern text from the class function documentation and PHP code examples.

Documentation

RA-KMP-Search

The RA_KMP_search class provides an implementation of the Knuth-Morris-Pratt (KMP) algorithm

Author: Roberto Aleman, ventics.com

RA_KMP_search Class Documentation (PHP)

Overview

The RA_KMP_search class provides an implementation of the Knuth-Morris-Pratt (KMP) algorithm, which is an efficient string searching algorithm. It allows you to find all occurrences of a given pattern within a text. The KMP algorithm optimizes the search process by pre-processing the pattern to determine the largest possible shift in case of a mismatch, thus avoiding redundant comparisons.

Algorithm Explanation (Brief)

The Knuth-Morris-Pratt algorithm works by using information about the pattern itself to decide where to continue the search when a mismatch occurs. It pre-computes an array called the LPS (Longest Proper Prefix which is also a Suffix) array. For each index i in the pattern, LPS[i] stores the length of the longest proper prefix of the pattern that is also a suffix of the pattern up to index i. This information is then used during the search to efficiently shift the pattern when a mismatch happens in the text.

Class Methods

public function search(string $text, string $pattern): array

This method searches for all occurrences of the specified pattern within the given text using the KMP algorithm.

Parameters:

  • `string $text`: The text to search within.
  • `string $pattern`: The pattern to search for.

Return Value:

  • `array`: An array containing integer values representing the starting index (0-based) of each occurrence of the pattern found in the text. If the pattern is not found, an empty array is returned.

Example Usage:

<hr>

require_once 'RA_KMP_search.php'; // Assuming the class is in a file named KMP_search.php

$kmpSearch = new RA_KMP_search();

$text1 = "ABABDABACDABABCABAB"; $pattern1 = "ABABCABAB"; $result1 = $kmpSearch-&gt;search($text1, $pattern1);

echo "Text: " . $text1 . "\n"; echo "Pattern: " . $pattern1 . "\n"; echo "Occurrences found at positions: "; if (empty($result1)) { echo "None\n"; } else { echo implode(", ", $result1) . "\n"; }

echo "\n";

$text2 = "aaaaa"; $pattern2 = "aa"; $result2 = $kmpSearch-&gt;search($text2, $pattern2);

echo "Text: " . $text2 . "\n"; echo "Pattern: " . $pattern2 . "\n"; echo "Occurrences found at positions: "; if (empty($result2)) { echo "None\n"; } else { echo implode(", ", $result2) . "\n"; }

echo "\n";

$text3 = "The quick brown fox jumps over the lazy fox."; $pattern3 = "fox"; $result3 = $kmpSearch-&gt;search($text3, $pattern3);

echo "Text: " . $text3 . "\n"; echo "Pattern: " . $pattern3 . "\n"; echo "Occurrences found at positions: "; if (empty($result3)) { echo "None\n"; } else { echo implode(", ", $result3) . "\n"; } </hr>

ATENTION!

If you require further explanation, I can assist you based on my availability and at an hourly rate.

If you need to implement this version or an advanced and/or customized version of my code in your system, I can assist you based on my availability and at an hourly rate.

Do you need advice to implement an IT project, develop an algorithm to solve a real-world problem in your business, factory, or company?

Write me right now and I'll advise you.

Roberto Aleman, ventics.com


  Files folder image Files (3)  
File Role Description
Plain text file LICENSE Lic. License text
Plain text file RA_KMP_search.php Class Class source
Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0