Home > Backend Development > PHP Tutorial > How to calculate string similarity using similar_text method in php

How to calculate string similarity using similar_text method in php

墨辰丷
Release: 2023-03-30 17:32:02
Original
1516 people have browsed it

This article mainly introduces the similar_text method in PHP to calculate the similarity of strings. Interested friends can refer to it. I hope it will be helpful to everyone.

The details are as follows:

<?php 
/*
 *
 * @param string $title_1 题目1
 * @param string $title_2 题目2
 * @return float $percent 相似百分比
 */
function title_similar($title_1,$title_2) {
  $title_1 = get_real_title($title_1);
  $title_2 = get_real_title($title_2);
  similar_text($title_1, $title_2, $percent);
  return $percent;
}
/**
 * php采集文章题目并去版权
 * @param string $html 需要采集的html源代码
 * @return string
 */
function get_real_title($str){
  $str = str_replace(array(&#39;-&#39;,&#39;—&#39;,&#39;|&#39;),&#39;_&#39;,$str);
  $splits = explode(&#39;_&#39;, $str);
  $l = 0;
  foreach ($splits as $tp){
    $len = strlen($tp);
    if ($l < $len){$l = $len;$tt = $tp;}
  }
  $tt = trim(htmlspecialchars($tt));
  return $tt;
}
//以下是测试
$title_1 = &#39;代号PHPCMS V9产品正式发布公测版本&#39;;
$title_2 = &#39;PHPCMS再战江湖 V9产品正式发布公测版本&#39;;
$percent = title_similar($title_1,$title_2);
echo &#39;相似百分比:&#39;.$percent.&#39;%&#39;;
echo "<br />\n";
?>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Definition and usage skills of smarty custom resources

How to obtain users in PHP IP address

php usage history function

The above is the detailed content of How to calculate string similarity using similar_text method in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template