基于PHP/CURL/codeIgniter的Spider Webbot爬虫[5]-经常使用字符串处理函数式

stristr(haystack,needle) 区分大小写 干草堆和缝衣针//判断haystack中是否存在needle。输出bool真假
strstr不区分大小写函数

strip_tags($string) 移除其中的HTML和PHP标记
str_replace("\t","",$string)//移除定位符号
str_replace(" ","",$string)//移除连续的空白
str_replace("\n","",$string)//移除换行符号code

similar_text($a,$b);统计两个string的相同字符数
echo similar_text('first', 'second',$persent);
echo $persent;//输出类似的百分比ip

自建函数
str_compare($a,$b)
//若是$b被$a彻底包含,则类似度会输出100%
//是用similar_text的话,会输出一个较低的百分比string

/*
//str_compare($a,$b);
//加强版的similar_text,能够对彻底包容的字符进行容错,若是$2被$1彻底包含,则输出100%
function str_compare($str1, $str2) {
$count = 0;io

$str1 = preg_replace("[^a-z]", ' ', strtolower($str1));
while(strstr($str1, '  ')) {
    $str1 = str_replace('  ', ' ', $str1);
}
$str1 = explode(' ', $str1);

$str2 = preg_replace("[^a-z]", ' ', strtolower($str2));
while(strstr($str2, '  ')) {
    $str2 = str_replace('  ', ' ', $str2);
}
$str2 = explode(' ', $str2);

if(count($str1)<count($str2)) {
    $tmp = $str1;
    $str1 = $str2;
    $str2 = $tmp;
    unset($tmp);
}

for($i=0; $i<count($str1); $i++) {
    if(in_array($str1[$i], $str2)) {
        $count++;
    }
}

return $count/count($str2)*100;

}
*/function

相关文章
相关标签/搜索