Blogger Information
Blog 14
fans 0
comment 0
visits 27730
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
获取相同的字符(字母)
花式BUG_beisi
Original
1030 people have browsed it

/** 

 * 找两个字符串相同的部分 

 **/  

function main($str1, $str2) {  

    //将字符串转成数组  

    $arr1 = str_split($str1);  

    $arr2 = str_split($str2);  

    //计算字符串的长度  

    $len1 = strlen($str1);  

    $len2 = strlen($str2);  

    //初始化相同字符串的长度  

    $len = 0;  

    //初始化相同字符串的起始位置  

    $pos = -1;  

    for ($i = 0; $i < $len1; $i++) {  

        for ($j = 0; $j < $len2; $j++) {  

            //找到首个相同的字符  

            if ($arr1[$i] == $arr2[$j]) {  

                //判断后面的字符是否相同  

                for ($p = 0; (($i + $p) < $len1) &&   

                    (($j + $p) < $len2) &&   

                    ($arr1[$i + $p] == $arr2[$j + $p]) &&   

                    ($arr1[$i + $p] <> ''); $p++);  

                if ($p > $len) {  

                    $pos = $i;  

                    $len = $p;  

                }  

            }  

        }  

    }   

    if ($pos == -1) {  

        return ;  

    } else {  

        return substr($str1, $pos, $len);  

    }  

}  

echo main("fgfdgsdgd东方大饭店gd对方答复sqq", "qqqqqqqqqqqqg大qqqq对qqqq)");  


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post