Matching Strings in Zend PHP 5

Here is an overview of the matching strings in Zend PHP 5 section when studying for the certification. There are a variety of functions in PHP we can use to compare and match two strings. Below are some examples, such as the equal comparison operators used to compare if two strings are equal.

$string1 = 'string';
$string2 = 'something else';

var_dump($string1 == $string2); // Outputs: boolean false;

$variable1 = 45;
$variable2 = '45';

var_dump($variable1 == $variable2);  // Outputs: boolean true
var_dump($variable1 === $variable2); // Outputs: boolean false

More examples of comparison operators can be found here.

strcmp

A case-sensitive function that returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

$var1 = "Hello";
$var2 = "Hello";

echo strcmp($var1, $var2); // Outputs: 0

$var3 = "hello";

echo strcmp($var1, $var3); // Outputs: -32

Note that the output is not -1 or 1 if the left argument is less or more than the right argument. The numbers generated are a result of subtracting the ASCII value of the first character that does not match of both arguments via the ord() function.

So for the example above:

ord('H'); // 72
ord('h'); // 104

72 - 104 = -32

Also note that the function is type insensitive, so the following outputs 0:

$var1 = '123';
$var2 = 123;

echo strcmp($var1, $var2); // Outputs: 0

strcasecmp

The strcasecmp() function is the case insensitive version of strcmp(), so the following would output 0 unlike the same example above using strcmp().

$var1 = 'Hello';
$var2 = 'hello';

echo strcmp($var1, $var2); // 0

similar_text

The similar_text() function calculates the similarity between two strings. By passing in two parameters, similar_text() returns the number of characters that are found in each argument.

<?php 
$var_1 = 'PHP IS GREAT'; 
$var_2 = 'WITH MYSQL'; 

echo similar_text($var_1, $var_2); // Outputs: 3

Note that you can also pass in a third parameter, $percent, that will calculate the similarity in percent for you.

<?php 
$var_1 = 'PHP IS GREAT'; 
$var_2 = 'WITH MYSQL'; 

similar_text($var_1, $var_2, $percent); 

echo $percent; // Outputs: 27.272727272727

Be aware that using similar_text() on two empty strings will return 0. Another thing to be aware of is when using this function, that the order of passing the strings is very important if you want to calculate the percentage of similarity, in fact, altering the variables will give a very different result. An example being:

<?php 
$var_1 = 'PHP IS GREAT'; 
$var_2 = 'WITH MYSQL'; 

similar_text($var_1, $var_2, $percent); 

echo $percent; 
// 27.272727272727 

similar_text($var_2, $var_1, $percent); 

echo $percent; 
// 18.181818181818 

levenshtein

The levenshtein() function calculates the Levenshtein distance between two strings.

The Levenshtein distance is defined as the minimal number of characters you have to replace, insert or delete to transform str1 into str2.

In its simplest form the function will take only the two strings as parameter and will calculate just the number of insert, replace and delete operations needed to transform str1 into str2.

$str1 = 'carrrot';
$str2 = 'carrot';

echo levenshtein($str1, $str2); // Outputs: 1

Additional parameters include:

  • cost_ins – Defines the cost of insertion.
  • cost_rep – Defines the cost of replacement.
  • cost_del – Defines the cost of deletion.

The below example defined the insert, replace and delete cost for $string1 to be converted to $string2.

$string1 = "Hello World"
$string2 = "ello World"
echo levenshtein($string1, $string2, 10, 20, 30); // Outputs: 30

This is because we have to delete the “H” from $string1 for the string to be converted into $string2.

Note: This article is based on PHP version 5.5.