Php is the simplest string matching algorithm. Php: the simplest string matching algorithm. This article describes the simplest string matching algorithm in php. Share it with you for your reference. The specific implementation method is as follows: php's simplest string matching algorithm and php matching algorithm
This article describes the simplest string matching algorithm in php. Share it with you for your reference. The specific implementation method is as follows:
The code is as follows:
<? Php
/*
Php implementation of the simplest string matching algorithm
T: ababcabc
P: abc
0. 1. 2.
Ababcabc
|
Abc
(X) (O)
3. 4. 5.
Ababcabc
|
Abc
(X) (O)
*/
$ Str = "ababcabc ";
$ Search = "abc ";
$ Strlen = strlen ($ str );
$ Searchlen = strlen ($ search );
// 1. Traverse $ str string
For ($ I = 0; $ I <$ strlen; $ I ++ ){
If ($ I + $ searchlen> $ strlen ){
Echo 'beyond length'; break;
}
$ Match = true;
// 2. traverse the string $ search to be searched for comparison
For ($ j = 0; $ j <$ searchlen; $ j ++ ){
If ($ str [$ I + $ j]! = $ Search [$ j]) {
$ Match = false;
Break;
}
$ K = $ I + $ j;
If ($ match ){
If ($ j = $ searchlen-1 ){
Echo "{$ str} starting from position {$ I}: {$ search} \ n"; break;
}
}
}
}
?>
I hope this article will help you with PHP programming.
Examples in this article describes the simplest string matching algorithm in php. Share it with you for your reference. The specific implementation method is as follows :...