compare two strings in php and show character differences -


hi struggling right:

i want compare 2 strings , calculate score in php.

what means have 2 strings:

$string1="example1"; $string2="exumple22";

now want compare strings if equal - in case not.

but additionally want see characters match. @ least count them.

in case be: 6;

i have tried stuck hers example:

enter code here <?   include("connect.php"); $query="select * data 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 case-sensitive string comparison <br>"; $j = strlen($string1);     ($i = 0; $i < $j; $i++) {     $stringtemp1++;     echo $string1[$i].', ';     echo $stringtemp1;     }          $u = strlen($string2);     ($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);         ($r = 0; $r < $e; $r++) {         $stringtemp1++;         echo $string1[$r].', ';         echo $stringtemp1; }$scorea=$stringtemp1; $stringtemp1=0; }  ?> 

see http://www.php.net/manual/en/function.similar-text.php how use similar_text($str1, $str2)

this give number of matching chars in both strings.

<?php     echo similar_text("hello world","hello peter"); ?> 

will give 7 (the number of characters in common).


Comments