MemoryRefresh!

String / Comparing     Comparing



Questions 1 Answers 0%

Pages   0 from 91
Questions   0 from 142

Reset


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 pronunciation rules // limited global use echo soundex('programming') == soundex('programmer'); // TRUE // P626 == P626 echo metaphone('programming') != metaphone('programmer'); // TRUE // PRKRMNK != PRKRMR

Related Pages

Basics
Comparing
Searching
Parse
Replacing
Formatting
Printf
Regexp
Datetime


0% 100%
 
 
0 pages 91 pages
1) What's the output? <?php echo strcmp("123abc", 123);




2) What's the output? <?php echo strncmp(123, "123abc");







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 pronunciation rules // limited global use echo soundex('programming') == soundex('programmer'); // TRUE // P626 == P626 echo metaphone('programming') != metaphone('programmer'); // TRUE // PRKRMNK != PRKRMR

References









Programming

Php
   
Regex
   
MySQL
   
Css
   
jQuery
   
Git
   


References