php - 比较php中的两个字符串并显示字符差异

标签 php string compare character match

您好,我正在努力解决这个问题:

我想比较两个字符串并在 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/

相关文章:

php - 将值计算从 SQL 移到 PHP 层是否更好?

java - 是否可以通过在 for 循环中调用字符串名称来检查数百个字符串数组?

java - 迭代 List<String> 并修改 String

php - php print_r 语句末尾的 "1"是什么意思?

php - 在 MediaWiki 上使用广告和横幅

c - 在声明中搜索我的名字!在c程序中使用字符串函数

r - 比较 R 中的数据帧

algorithm - 确定 Treeview 中的最小变化

python - 使用 Python 的列表列表的差异

php - 这个 json 可能在 jquery 中输出为数组吗?