您好,欢迎访问一九零五行业门户网

分析PHP的similar_text函数_PHP教程

//比较字串,返回两个字串第一个相同字符的区域
static void php_similar_str(const char *txt1, int len1, const char *txt2, int len2, int *pos1, int *pos2, int *max)
{
 char *p, *q;
 char *end1 = (char *) txt1 + len1;
 char *end2 = (char *) txt2 + len2;
 int l;
*max = 0;
 //遍历字串
 for (p = (char *) txt1; p   for (q = (char *) txt2; q    for (l = 0; (p + l    if (l > *max) {
    //保存相同区域信息
    *max = l;
    *pos1 = p - txt1;
    *pos2 = q - txt2;
   }
  }
 }
}
//递归函数,比较txt1和txt2的相同字符数量
static int php_similar_char(const char *txt1, int len1, const char *txt2, int len2)
{
 int sum;
 int pos1, pos2, max;
 php_similar_str(txt1, len1, txt2, len2, &pos1, &pos2, &max);
 if ((sum = max)) {//这样写有什么意义?!!!!!
  //递归上次不同部分的前部
  if (pos1 && pos2) {
   sum += php_similar_char(txt1, pos1, txt2, pos2);
  }
  //递归上次不同部分的后部
  if ((pos1 + max    sum += php_similar_char(txt1 + pos1 + max, len1 - pos1 - max,
      txt2 + pos2 + max, len2 - pos2 - max);
  }
 }
 //返回本次比较后的相同字符数量
 return sum;
}
//php函数本身,一堆宏,做了些串转换和返回值换算,主功能由上面两个函数做了。
php_function(similar_text)
{
 zval **t1, **t2, **percent;
 int ac = zend_num_args();
 int sim;
if (ac 3 || zend_get_parameters_ex(ac, &t1, &t2, &percent) == failure) {
  wrong_param_count;
 }
 convert_to_string_ex(t1);
 convert_to_string_ex(t2);
 if (ac > 2) {
  convert_to_double_ex(percent);
 }
if (z_strlen_pp(t1) + z_strlen_pp(t2) == 0) {
  if (ac > 2) {
   z_dval_pp(percent) = 0;
  }
  return_long(0);
 }
sim = php_similar_char(z_strval_pp(t1), z_strlen_pp(t1), z_strval_pp(t2), z_strlen_pp(t2));
 if (ac > 2) {
  z_dval_pp(percent) = sim * 200.0 / (z_strlen_pp(t1) + z_strlen_pp(t2));
 }
 return_long(sim);
}
所有看出similar_text是根据ascii做比较的,是不考虑词组问题的。
测试了一下,结果如下:
int main(int argc, char *argv[])
{
  char *str1=weican wuxin;
  char *str2=wuxin weican;
  int pos1,pos2,max;
php_similar_str(str1,strlen(str1),str2,strlen(str2),&pos1,&pos2,&max);
  printf(php_similar_str:%d,%d=%d,pos1,pos2,max);
max=php_similar_char(str1,strlen(str1),str2,strlen(str2));
  printf(php_similar_char:%d,max);
system(pause);
  return 0;
}
php_similar_str:0,6=6
php_similar_char:6
char *str1=tcp协议通讯工作原a理;
char *str2=tcpa协议通讯工作原理;
php_similar_str:3,4=14
php_similar_char:19
http://www.bkjia.com/phpjc/508234.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/508234.htmltecharticle//比较字串,返回两个字串第一个相同字符的区域 static void php_similar_str(const char *txt1, int len1, const char *txt2, int len2, int *pos1, int *pos2, int *max)...
其它类似信息

推荐信息