Page 66 - Open Soource Technologies 304.indd
P. 66
Web Technologies-I
Notes All string comparisons take at least two arguments and return a value based on comparing those
arguments. The return value is always an integer that can be interpreted as follows:
0 (zero): the two values are equal
> 0 (greater than zero): value two is greater than value one
< 0 (less than zero): value one is greater than value two
Our basic set of string comparison functions are as follows:
strcmp (str1, str2);
Takes two arguments and compares them as strings. In order for any of these functions to work,
both arguments must be values that can be cast to strings. The strings are compared in ASCII order.
strcasecmp (str1, str2);
Takes two arguments and compares them as strings. The strings are compared in ASCII order. But
without regard to case sensitivity, all alphabetic characters are treated as if they were lower-case.
strnatcmp (str1, str2);
Takes two arguments and compares them as strings. The strings are compared in ASCII
order, except for numbers, which are compared in natural order, which is to say, as numbers.
This is true even if they occur in the middle of the string. Thus ‘February 2’ will evaluate to
less than ‘February 14’, whereas on a normal string comparison it would be greater since 2
is greater than 1.
strnatcasecmp (str1, str2);
This combines the previous two functions and performs a case-insensitive search with numbers
sorted in natural order.
strncmp (str1, str2, length);
Takes three arguments, two strings to compare and the length to which to compare them. This
allows you to perform a comparison on just the beginning portion of a string from index position
zero to the specified length.
strncasecmp (str1, str2, length);
This is the same as the previous function except that it performs a case-insensitive search.
3.2.2 Similarity
PHP also provides numerous ways of comparing strings to see whether they are similar. We can
compare for textual similarity and for phonic similarity.
Phonic Similarity
Phonic similarity first, since it is a little easier.
Phonic similarity just means that things are compared based on whether they sound alike. To
compare things by how they sound you can use one of two functions.
The two functions are:
• soundex()
• metaphone()
Both return strings that represent the pronunciation of a word, you can use the result values in
comparisons for equality.
60 LOVELY PROFESSIONAL UNIVERSITY