Digipng Digipng - 1 month ago 5x
PHP Question

How can I count the number of differences between the two string in php?

I want to compare two string due to length of shorter string and return number of differences theme. for example:

$first_str = "abcdez";
$second_str= "abhdx";

Now due to length of
is shorter So must be return 2 Because they differ only in the third and fourth index. In fact are compared equal index.
Is there a function for this work in php? if not, can you help me for writing code ?


Maybe this helps. I forked the comment from Anant.

$first_str = "abcdez";
$second_str= "abhdx";

$first_str_array = str_split($first_str);

$second_str_array = str_split($second_str);

$first_count = count($first_str_array);
$second_count = count($second_str_array);

if (($first_count - $second_count) > 0) {
    $count = $first_count - $second_count;
} else if (($second_count - $first_count) > 0){
    $count = $second_count - $first_count;

$final_difference = array_diff($first_str_array,$second_str_array);