嗨,我正在努力做到这一点:
我想比较两个字符串并在PHP中计算他们的分数.
这意味着我有两个字符串:
$字符串1 = “例1”;
$字符串2 = “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;
- }
- ?>
有关如何使用similar_text($str1,$str2),请参阅
http://www.php.net/manual/en/function.similar-text.php
这将为您提供两个字符串中匹配字符的数量.
- <?PHP
- echo similar_text("Hello World","Hello Peter");
- ?>
会给你7(共同的字符数).