This article mainly introduces the simplest String Matching Algorithm in php, which can be used to search for strings and display matching positions. It is a practical string operation technique. For more information, see
This article mainly introduces the simplest String Matching Algorithm in php, which can be used to search for strings and display matching positions. It is a practical string operation technique. For more information, see
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.
,