PHP docs
| Id/Actions | Category | Code Examples | Test Code | Links | Remarks |
|---|---|---|---|---|---|
| 41 View |
String Functions - 27. levenshtein — Calculate Levenshtein distance between two strings | <?php
// int levenshtein ( string $str1 , string $str2 )
// int levenshtein ( string $str1 , string $str2 ,
// int $cost_ins , int $cost_rep , int $cost_del )
// input misspelled word
$input = 'carrrot';
// array of words to check against
$words = array('apple','pineapple','banana','orange',
'radish','carrot','pea','bean','potato');
// no shortest distance found, yet
$shortest = -1;
// loop through words to find the closest
foreach ($words as $word) {
// calculate the distance between the input word,
// and the current word
$lev = levenshtein($input, $word);
// check for an exact match
if ($lev == 0) {
// closest word is this one (exact match)
$closest = $word;
$shortest = 0;
// break out of the loop; we've found an exact match
break;
}
// if this distance is less than the next found shortest
// distance, OR if a next shortest word has not yet been found
if ($lev <= $shortest || $shortest < 0) {
// set the closest match, and shortest distance
$closest = $word;
$shortest = $lev;
}
}
echo "Input word: $input\n";
if ($shortest == 0) {
echo "Exact match found: $closest\n";
} else {
echo "Did you mean: $closest?\n";
}
|
Input word: carrrot Did you mean: carrot? | http://www.php.net/manual/en/function.levenshtein.php | |
| 42 View |
String Functions - 28. localeconv — Get numeric formatting information | <?php
// array localeconv ( void )
if (false !== setlocale(LC_ALL, 'nl_NL.UTF-8@euro')) {
$locale_info = localeconv();
print_r($locale_info); // the constant does not work -- jli
}
?>
|
http://www.php.net/manual/en/function.localeconv.php | ||
| 43 View |
String Functions - 29. ltrim — Strip whitespace (or other characters) from the beginning of a string | <?php // string ltrim ( string $str [, string $charlist ] ) $text = "\t\tThese are a few words :) ... "; $binary = "\x09Example string\x0A"; $hello = "Hello World"; var_dump($text, $binary, $hello); print "\n"; $trimmed = ltrim($text); var_dump($trimmed); $trimmed = ltrim($text, " \t."); var_dump($trimmed); $trimmed = ltrim($hello, "Hdle"); var_dump($trimmed); // trim the ASCII control characters at the beginning of $binary // (from 0 to 31 inclusive) $clean = ltrim($binary, "\x00..\x1F"); var_dump($clean); |
string(32) " These are a few words :) ... " string(16) " Example string " string(11) "Hello World" string(30) "These are a few words :) ... " string(30) "These are a few words :) ... " string(7) "o World" string(15) "Example string " | http://www.php.net/manual/en/function.ltrim.php | |
| 44 View |
String Functions - 30. md5_file — Calculates the md5 hash of a given file | <?php // string md5_file ( string $filename [, bool $raw_output = false ] ) $file = 'date.txt'; echo 'MD5 file hash of ' . $file . ': ' . md5_file($file); |
MD5 file hash of date.txt: e10208f8f7fcc7abfebbaa11e0a5d9a5 | http://www.php.net/manual/en/function.md5-file.php | |
| 45 View |
String Functions - 30. md5_file — Calculates the md5 hash of a given file | <?php // string md5_file ( string $filename [, bool $raw_output = false ] ) $file = 'date.txt'; echo 'MD5 file hash of ' . $file . ': ' . md5_file($file); |
MD5 file hash of date.txt: e10208f8f7fcc7abfebbaa11e0a5d9a5 | http://www.php.net/manual/en/function.md5-file.php |
Page 9 of 10, showing 5 records out of 46 total, starting on record 41, ending on 45
