PHP String comparison Methods

From Webmaster-A Webmaster Resources

PHP built in String Comparisons (==,strcmp(),strcasecmp() )

The simplest string comparison method is to use the built in comparison operator "==" thus:
if ($string1 == $string2) do some action

The main drawback with this is that the program will produce unreliable results with many types of operands. Lets say for example you end up comparing a boolean value (true/False) or an integer with a string. What will the final result be? To combat this problem PHP has what is known as binary safe functions, where the function 'knows' what type of variable it is supposed to return.

strcmp()Used like This:
$result=strcmp($string1, $string2);

This function returns less than 0 if string1 is less than string2, greater than 0 if string1 is greater than string2 , and 0 if the strings are equal.

strcasecmp()Used like This:
$result=strcasemp($string1, $string2);

This is a case-insensitive version of strcmp and should be used where taking into account character case is not desirable, it returns exactly the same results as the PHP strcmp function. strncmp() Used like this:
$result=strncmp($string1, $string2,$int_num_chars);

This is the same as strcmp() except it obnly looks at the first int_num_chars or both strings.

strncasecmp() Used like this:
$result=strncasecmp($string1, $string2,$int_num_chars);

This is the same as strcnmp() it is case insensitive.

Why make your own Special String comparison Method?

PHP has its own methods for string comparison so why make your own? The answer is simple, there are many situations where although the compared strings are not the same, for your program purposes they are! For example:
Southall Estate = Southall Housing Estate =Housing Estate in Southall
big dog=big black dog=big strong dog=big aggressive dog
This requires some sort of intelligent comparison or fuzzy logic matching which PHP does not provide!

String1:

String2:

Result:

Interpretation:The string is the same.

This above is the output from the string comparison scoring function shown below. It is surprising when you test it how bulletproof the comparison method is as regards selecting similar and dissimilar strings. I have used it and other methods like it many times. You are free to use this PHP string comparison code yourself, the code has been placed in a text box to make cut and paste easier. A link back to www.webmaster-a.com would be appreciated!

Fuzzy Logic String Comparison Code

This function intelligently compares two strings, and scores them on similarity.

To use it you can put in sample strings on this page and decide on a threshold value for the score which indicates a level of similarity to the strings which corresponds to the level of recognition you need.


Useful Pages

Social Bookmarking:



PHP Unique Filename Referrers

bookmark hit
internal hit
www.bing.com
...
yandex.ru
...
www.phone-no.net
...
www.watchformen.biz
www.perfumefor.net
...
...
...
...
...
www.musicstorein.biz
mp3span.com
...
www.hgt435lop345.com
...
www.health-4.com
search.yahoo.com
duckduckgo.com

This list of people who link to this PHP Code Snippet page is automagically maintained. Link to us, send traffic, and get listed. Simple.