This article mainly gives you a method to synthesize the longest common string from two strings using PHP. Involving php string and array traversal, operation, judgment and other related operation skills.
The code is as follows:
<?php $a = 'abceee12345309878'; $b = 'abceeew2345i09878fsfsfsfabceeewsfsdfsfsabceeew'; $c = array(); $lenht1 = strlen($a); $lenth2 = strlen($b); $startTime = microtime(true); for ($i=0;$i<$lenht1;$i++) { for ($j=0;$j<$lenth2;$j++) { $n = ($i-1>=0 && $j-1>=0)?$c[$i-1][$j-1]:0; $n = ($a[$i] == $b[$j]) ? $n+1:0; $c[$i][$j] = $n; } } foreach ($c as $key=>$val) { $max = max($val); foreach ($val as $key1 =>$val1) { if ($val1 == $max && $max>0) { $cdStr[$max] = substr($b,$key1-$max+1,$max); } } } ksort($cdStr); $endTime = microtime(true); echo "Totle time is " . ($endTime - $startTime) . " s"."<br/>"; print_r(end($cdStr)); exit; ?>
Running results:
Totle time is 0.0012800693512 s abceee
I believe you have mastered the method after reading these cases. For more exciting information, please pay attention to php Chinese Other related articles online!
Related reading:
How to generate Cartesian product by PHP custom function
How does PHP solve the problem of large website traffic and High concurrency
Detailed explanation of javascript data types and git usage code
The above is the detailed content of How to find the longest common substring of two strings in PHP?. For more information, please follow other related articles on the PHP Chinese website!