很多站长朋友们都不太清楚php生成相似字,今天小编就来给大家整理php生成相似字,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php在数组中查询字符串相似度并返回键名或者键值 2、 有人用php实现文本相似度算法吗 3、 PHP如何计算两个字符串的相似度? 4、 php比较相似字符串的方法 5、 php数组提取拥有相同字符的数,新生成一个数组 6、 php 怎么匹配两个字符串的相似度 php在数组中查询字符串相似度并返回键名或者键值根据传入的字符串和数组,返回数组中相似度最高的字符串
PHP代码如下:
1.function closest_word($input, $words) {
2. $shortest = -1;
3. foreach ($words as $word) {
4. $lev = levenshtein($input, $word);
5. if ($lev == 0) {
6. $closest = $word;
7. $shortest = 0;
8. break;
9. }
10. if ($lev <= $shortest || $shortest < 0) {
11. $closest = $word;
12. $shortest = $lev;
13. }
14. }
15. return $closest;
16.}
2. 代码示例如下:
// 根据传入的州名(可能客户有输错),返回相似度最高的州名称
$united_state_list = array(
'AL'=>"Alabama",
'AK'=>"Alaska",
'AZ'=>"Arizona",
'AR'=>"Arkansas",
'CA'=>"California",
'CO'=>"Colorado",
'CT'=>"Connecticut",
'DE'=>"Delaware",
'DC'=>"District Of Columbia",
'FL'=>"Florida",
'GA'=>"Georgia",
'HI'=>"Hawaii",
'ID'=>"Idaho",
'IL'=>"Illinois",
'IN'=>"Indiana",
'IA'=>"Iowa",
'KS'=>"Kansas",
'KY'=>"Kentucky",
'LA'=>"Louisiana",
'ME'=>"Maine",
'MD'=>"Maryland",
'MA'=>"Massachusetts",
'MI'=>"Michigan",
'MN'=>"Minnesota",
'MS'=>"Mississippi",
'MO'=>"Missouri",
'MT'=>"Montana",
'NE'=>"Nebraska",
'NV'=>"Nevada",
'NH'=>"New Hampshire",
'NJ'=>"New Jersey",
'NM'=>"New Mexico",
'NY'=>"New York",
'NC'=>"North Carolina",
'ND'=>"North Dakota",
'OH'=>"Ohio",
'OK'=>"Oklahoma",
'OR'=>"Oregon",
'PA'=>"Pennsylvania",
'RI'=>"Rhode Island",
'SC'=>"South Carolina",
'SD'=>"South Dakota",
'TN'=>"Tennessee",
'TX'=>"Texas",
'UT'=>"Utah",
'VT'=>"Vermont",
'VA'=>"Virginia",
'WA'=>"Washington",
'WV'=>"West Virginia",
'WI'=>"Wisconsin",
'WY'=>"Wyoming"
);
$input_state = 'Wiscsin';
$state = closest_word($input_state ,array_values($united_state_list));
echo $state;
有人用php实现文本相似度算法吗本文实例讲述了PHP简单实现文本计数器的方法。分享给大家供大家参考,具体如下:
<?php if (file_exists('count_file.txt')) { $fil = fopen('count_file.txt', r); $dat = fread($fil, filesize('count_file.txt')); echo $dat+1; fclose($fil); $fil = fopen('count_file.txt', w); fwrite($fil, $dat+1); } else { $fil = fopen('count_file.txt', w); fwrite($fil, 1); echo '1'; fclose($fil); } ?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php正则表达式用法总结》、《PHP+ajax技巧与应用小结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
PHP如何计算两个字符串的相似度?similar_text('my name is php', 'my name is java', $result);
var_dump($result);
参考链接:网页链接
php比较相似字符串的方法本文实例讲述了php比较相似字符串的方法。分享给大家供大家参考。具体分析如下:
这里通过php的similar_text函数比较两个字符串的相似性。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$word2compare
=
stupid;
$words
=
array(
'stupid',
'stu
and
pid',
'hello',
'foobar',
'stpid',
'upid',
'stuuupid',
'sstuuupiiid',
);
while(list($id,
$str)
=
each($words)){
similar_text($str,
$word2compare,
$percent);
Comparing
'$word2compare'
with
'$str':
;
round($percent)
.
%n;
}
/*
Results:
Comparing
'stupid'
with
'stupid':
100%
Comparing
'stupid'
with
'stu
and
pid':
71%
Comparing
'stupid'
with
'hello':
0%
Comparing
'stupid'
with
'foobar':
0%
Comparing
'stupid'
with
'stpid':
91%
Comparing
'stupid'
with
'upid':
80%
Comparing
'stupid'
with
'stuuupid':
86%
Comparing
'stupid'
with
'sstuuupiiid':
71%
*/
希望本文所述对大家的php程序设计有所帮助。
php数组提取拥有相同字符的数,新生成一个数组$id = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2'];
$key = ['A', 'B', 'C'];
foreach ($key as $k) {
$$k = [];
}
foreach ($id as $v) {
foreach ($key as $k) {
if (stripos($v, $k) === 0) {
array_unshift($$k, $v);
}
}
}
foreach ($key as $v){
var_dump($$v);
}
主要是使用变量追加数组array_unshift, 你可以尝试运行一下 理解理解
php 怎么匹配两个字符串的相似度php自带一个函数similar_text,可以计算两个字符串的相似度,但是这个的准确性、速度不是很好。网上有很多其他的方法和现成的包,你可以搜索看看。下面简单列举一个类
class LCS {
var $str1;
var $str2;
var $c = array();
/*返回串一和串二的最长公共子序列*/
function getLCS($str1, $str2, $len1 = 0, $len2 = 0) {
$this->str1 = $str1;
$this->str2 = $str2;
if ($len1 == 0) $len1 = strlen($str1);
if ($len2 == 0) $len2 = strlen($str2);
$this->initC($len1, $len2);
return $this->printLCS($this->c, $len1 - 1, $len2 - 1);
}
/*返回两个串的相似度*/
function getSimilar($str1, $str2) {
$len1 = strlen($str1);
$len2 = strlen($str2);
$len = strlen($this->getLCS($str1, $str2, $len1, $len2));
return $len * 2 / ($len1 + $len2);
}
function initC($len1, $len2) {
for ($i = 0; $i < $len1; $i++) $this->c[$i][0] = 0;
for ($j = 0; $j < $len2; $j++) $this->c[0][$j] = 0;
for ($i = 1; $i < $len1; $i++) {
for ($j = 1; $j < $len2; $j++) {
if ($this->str1[$i] == $this->str2[$j]) {
$this->c[$i][$j] = $this->c[$i - 1][$j - 1] + 1;
} else if ($this->c[$i - 1][$j] >= $this->c[$i][$j - 1]) {
$this->c[$i][$j] = $this->c[$i - 1][$j];
} else {
$this->c[$i][$j] = $this->c[$i][$j - 1];
}
}
}
}
function printLCS($c, $i, $j) {
if ($i == 0 || $j == 0) {
if ($this->str1[$i] == $this->str2[$j]) return $this->str2[$j];
else return "";
}
if ($this->str1[$i] == $this->str2[$j]) {
return $this->printLCS($this->c, $i - 1, $j - 1).$this->str2[$j];
} else if ($this->c[$i - 1][$j] >= $this->c[$i][$j - 1]) {
return $this->printLCS($this->c, $i - 1, $j);
} else {
return $this->printLCS($this->c, $i, $j - 1);
}
}
}
关于php生成相似字的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php生成相似字 php找出重复的字符串的详细内容...