The simplest string matching algorithm in php, php matching algorithm_PHP tutorial

WBOY
Release: 2016-07-13 10:11:40
Original
1619 people have browsed it

The simplest string matching algorithm in php, php matching algorithm

The example in this article describes the simplest string matching algorithm in php. Share it with everyone for your reference. The specific implementation method is as follows:

Copy code The code is as follows:
/*
The simplest string matching algorithm php implementation

T: ababcabc
P: abc

0. ababcabc ababcabc ababcabc
|||    |||    |||
abc abc abc
(X)       (X)       (O)

3. 4. 5.
ababcabc ababcabc ababcabc
 
|||    |||    |||
 
abc abc abc
 
(X)       (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 'Exceeds length';break;
}

$match=true;

​  
//2. Traverse the string to be searched $search and compare
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}The {$i}th position starts with {$search}\n";break;
            }
          }
}
}
?>
I hope this article will be helpful to everyone’s PHP programming design.

http://www.bkjia.com/PHPjc/928222.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/928222.htmlTechArticleThe simplest string matching algorithm in php, php matching algorithm This article describes the simplest string matching algorithm in php matching algorithm. Share it with everyone for your reference. The specific implementation method is as follows:...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template