您好,我正在努力解决这个问题:
我想比较两个字符串并在 php 中计算它们的分数。
这意味着我有两个字符串:
$string1="例子1"; $string2="exumple22";
现在我想比较字符串是否相等 - 在本例中它们不相等。
但另外我想查看匹配的字符。至少数一数。
在这种情况下会是:6;
我试过这个但我卡住了她的例子:
enter code here
<?
include("connect.php");
$query="SELECT * FROM data where ID = '1'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
//echo "Score :{$row['scoreA']} <br>" ;
$scoretemp=$row['scoreA'];
$string1=$row['textA1'];
$string2=$row['textA2'];
}
mysql_close();
if (strcmp($string1, $string2) != 0){
echo "not equal in a case-sensitive string comparison <br>";
$j = strlen($string1);
for ($i = 0; $i < $j; $i++) {
$stringtemp1++;
echo $string1[$i].', ';
echo $stringtemp1;
}
$u = strlen($string2);
for ($t = 0; $t < $u; $t++) {
$stringtemp2++;
echo $string2[$t].', ';
echo $stringtemp2;
}$scoreA=($stringtemp1 - $stringtemp2);
$stringtemp1=0;$stringtemp2=0;
}
else{
echo "Stringmatch! <br>";
$e = strlen($string1);
for ($r = 0; $r < $e; $r++) {
$stringtemp1++;
echo $string1[$r].', ';
echo $stringtemp1;
}$scoreA=$stringtemp1;
$stringtemp1=0;
}
?>
最佳答案
参见 http://www.php.net/manual/en/function.similar-text.php如何使用 similar_text($str1, $str2)
这将为您提供两个字符串中匹配字符的数量。
<?php
echo similar_text("Hello World","Hello Peter");
?>
会给你7(共同的字符数)。
关于php - 比较php中的两个字符串并显示字符差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10903082/