Memory App
Programming
 




Strict

Not using strict operator === can lead to variable type problems.

$string = "123abc";

echo $string == 123 ? 'true' : 'false'; // true (incorect)

// "123abc" -> (int) "123abc" -> 123

echo $string === 123 ? 'true' : 'false'; // false (corect)
(1/6)

Safe

Binary safe string comparison. Returns < 0 if str1 is less than str2 Returns > 0 if str1 is greater than str2 Returns 0 if they are equal

echo strcmp("a", "a"); // 0

echo strcasecmp("A", "a"); // 0 (case insensitive)

echo strcmp("A", "a"); // -1

echo strcmp("a", "A"); // 1
Binary safe string comparison (n characters)

$str = "hello John";

echo strncasecmp($str, "Hello World", 5); // 0
substr_compare()

// substr_compare($main_str, $str, $offset, $length, $flag_case)

echo substr_compare("abcde", "bc", 1, 2); // bc, bc = 0

echo substr_compare("abcde", "de", -2, 2); // de, de = 0

echo substr_compare("abcde", "cd", 1, 2); // bc, cd = -1

echo substr_compare("abcde", "bc", 1, 3); // bcd, bc = 1

// main_str offset is less than str offset

echo substr_compare("abcde", "BC", 1, 2, true); // bc, bc = 0

// Third parameter - case insenstivity
    
echo substr_compare("abcde", "abc", 5, 1); // Warning

// Offset is equal or greater than string legth
(2/6)

Similar

similar_text() returns the number of matching chars in both strings

echo similar_text("cat", "can"); // 2

similar_text("cat", "can", $percent);

echo $percent; // 66,6
(3/6)

Levenshtein

Minimal number of characters you have to replace, insert or delete to transform str1 into str2.

echo levenshtein('cat', 'can'); // 1
(4/6)

Soundex

Words pronounced similarly produce the same soundex key, and can thus be used to simplify searches in databases where you know the pronunciation but not the spelling

echo soundex('wise'); // W200

echo soundex('ways'); // W200
(5/6)

Metaphone

More precise than soundex, but it is based in english pronunciation rules limited global use.

echo soundex('programming') == soundex('programmer'); // TRUE

// P626 == P626

echo metaphone('programming') != metaphone('programmer'); // TRUE

// PRKRMNK != PRKRMR


Questions



Top Reference > Programming
0/0 (80)  
Not Logged