思路: 最容易想到的方法,是把第一个字符串按顺序截取,与第二个字符串对比,存在则写入数组,最后再从数组找到重复之中最长的那个输出
字符串1:/a/b/c/?.oietr?e/f/g/zwty.cn
字符串2:/a/b/c/awp.neeg/e/f/g/zxtn.cc
$str1 = '/a/b/c/?.oietr?e/f/g/zwty.cn';
$str2 = '/a/b/c/awp.neeg/e/f/g/zxtn.cc';
$start=0;$end=2;
for($i=1;$i<strlen($str1);$i++){
$strpart = substr($str1,$start,$end);
if(strpos($str2,$strpart)==true){
$array[]=$strpart;
$end++;
}else{
$start=$i-1;
$end=2;
}
}
$result='';
$c = count($array);
for($j=0;$j<$c-1;$j++){
if(substr($array[$j+1],0,-1)!=$array[$j]){
$result .=$array[$j].'<br>';
$str_end=$array[$j];
}
}
if($array[$c-1]!=$str_end){
$result .=$array[$c-1];
}
echo $result;
运行之后:
a/b/c/
e/f/g/z
.c