• Javascript
  • Python
  • Go

Comparing Strings in PHP: Highlight the Difference

When it comes to comparing strings in PHP, there are a few different methods that can be used. One of the most useful techniques is to highl...

When it comes to comparing strings in PHP, there are a few different methods that can be used. One of the most useful techniques is to highlight the differences between two strings, which can be done using HTML tags formatting.

To start, let's define two strings that we want to compare:

$str1 = "Hello World";

$str2 = "Hello PHP";

Now, let's use the built-in function "similar_text()" to compare these two strings and highlight the differences. The syntax for this function is as follows:

similar_text($str1, $str2, $result);

The third parameter, $result, is optional and will store the percentage of similarity between the two strings. In this case, we want to use it to highlight the differences, so let's ignore it for now.

Next, we will use a loop to go through each character in both strings and compare them. If the characters are the same, we will simply print them out. However, if they are different, we will wrap them in HTML tags to highlight the difference.

Here's the full code for this comparison and highlighting process:

<?php

similar_text($str1, $str2, $result);

for ($i = 0; $i < strlen($str1); $i++) {

if ($str1[$i] == $str2[$i]) {

echo $str1[$i];

} else {

echo "<mark>" . $str1[$i] . "</mark>";

}

}

?>

The result of this code would be:

H<mark>e</mark>ll<mark>o</mark> <mark>W</mark><mark>o</mark>rld

As you can see, the differences between the two strings have been highlighted using the <mark> tag. This makes it easier to see where the strings differ and what characters have been changed.

But what if we want to compare two strings that are not the same length? In that case, we need to use a different function called "levenshtein()". This function calculates the minimum number of changes (insertions, deletions, or substitutions) required to turn one string into another. The syntax for this function is as follows:

levenshtein($str1, $str2);

Let's take a look at an example:

$str1 = "Hello World";

$str2 = "World Hello";

The result of using the levenshtein() function would be 10, indicating that it takes 10 changes to turn the first string into the second one. However, we can also use this function to highlight the differences between the two strings.

Here's the updated code:

<?php

$distance = levenshtein($str1, $str2);

for ($i = 0; $i < strlen($str1); $i++) {

if ($str1[$i] == $str2[$i]) {

echo $str1[$i];

} else {

echo "<mark>" . $str1[$i] . "</mark>";

}

}

for ($i = strlen($str1); $i < strlen($str2); $i++) {

echo "<mark>" . $str2[$i] . "</mark>";

}

?>

The result of this code would be:

H<mark>e</mark>llo <mark>Wor</mark>l<mark>d</mark

Related Articles

Extract Substring from String

Have you ever found yourself in a situation where you needed to extract a specific part of a string? Whether you're a developer or just a re...