Page 67 - Open Soource Technologies 304.indd
P. 67
Unit 3: Flow-Control Statements and PHP in Web Page
if (metaphone($a) == metaphone($b) { Notes
}
The only real purpose of these functions is to look for homophones, or words that sound alike.
This is something that is primarily of use if you are planning on writing spell-checkers and other
advanced text processing tools.
The result strings themselves are not pronounceable, but are strings to represent
the sound of the word in a standardized way. Of the two, metaphone() tends to
be more accurate because its algorithms for the rules of pronunciation are better.
Textual Similarity
Textual similarity is a little more useful because it allows you to compare how similar two text
strings are:
similar_text()
The similar_text() function takes two strings and returns an index value that returns the number
of characters that are the same, allowing for additions, subtraction, and repetition. It also takes
an optional third parameter which returns a value that represents the percentage of similarity.
similar_text($a, $b[, $percent]);
$match = similar_text($a, $b, $mper);
levenshtein()
The levenshtein() function returns a weighted score representing the difference between two
strings. It takes two strings and three optional arguments. The three optional arguments allow you
to specify how much weight to assign to different types of difference. This allows you to specify
how you want scores weighted with numeric values. Otherwise all scores are given equal weight.
Other different types are:
• insertions between string one and two
• replacements between string one and two
• deletions between strings one and two
levenshtein($a, $b[, $ins, $rep, $del])
$diff = levenshtein($a, $b, 100, 5, 1)
3.3 Iteration
Some statements in PHP are known as iterative statements. These statements have control
structures that delimit them and which determine how many times (zero or more) the delimited
code is executed, based on some condition.
We will look at three structures here:
• While statement and its variants
• Do ... while statement
• For statement
We will also look at some more advanced ways of working with iteration by being able to more
freely move between nested iterative statements.
LOVELY PROFESSIONAL UNIVERSITY 61