Studyon Minte9.com
ZCE 5.3

Study

Comparing



	// "123abc" == 123 (incorect, ===)
	// strcmp(A, a) = -1 | returns -n, 0, m | strcasecmp
	// strncmp(abc, ab, 2) = 0 | strncasecmp
	// substr_compare(abc, bc, 1) = 0 [, length, flagcase]

	// similar_text(cat, can) = 2 |  [, &percent] = 66.6
	// levenshtein(cat, can) = 1

	// soundex(wise) == soundex(ways)
	// metaphone(wise) != metaphone(ways) | more precise | english



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

<?php $string = "123abc"; echo $string == 123 ? 'true' : 'false'; // Output: true (incorect) // "123abc" -> (int) "123abc" -> 123 echo $string === 123 ? 'true' : 'false'; // Output: false (corect)
  Binary safe string comparison
<?php /* * 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)
<?php $str = "hello John"; echo strncasecmp($str, "Hello World", 5); // 0
  substr_compare()
<?php // 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
  similar_text() returns the number of matching chars in both strings
<?php echo similar_text("cat", "can"); // 2 similar_text("cat", "can", $percent); echo $percent; // 66,6
  levenshtein()
<?php // Minimal number of characters you have to replace, insert or delete // to transform str1 into str2 echo levenshtein('cat', 'can'); // 1
  soundex()
<?php // 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
  metaphone()
<?php // More precise than soundex, but it is based in english pronunciatino rules // limited global use echo soundex('programming') == soundex('programmer'); // TRUE // P626 == P626 echo metaphone('programming') != metaphone('programmer'); // TRUE // PRKRMNK != PRKRMR