Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php
  2.  
  3. //The user input search term
  4. $search = '90314';
  5.  
  6. //The term you're looking to match
  7. $truth = '90210';
  8.  
  9. //How many characters are matching
  10. $same = 0;
  11.  
  12. //How many characters are different (assume all at the start)
  13. $different = strlen($truth);
  14.  
  15. //The matching part of the two strings
  16. $matching = '';
  17.  
  18. //Iterate over each character of the term you're looking to match
  19. for($i = 0; $i < strlen($truth); $i++)
  20. {
  21.   //If the search string is long enough to have this
  22.   //character, and the character matches that of the
  23.   //term to be matched, this character is valid
  24.   if(isset($search[$i]) && $search[$i] == $truth[$i])
  25.   {
  26.     //Increment the count of similar characters
  27.     $same++;
  28.  
  29.     //Decrement the count of different characters
  30.     $different--;
  31.  
  32.     //Continue to the next character position
  33.     continue;
  34.   }
  35.   //If each the search string is too short, or the
  36.   //character doesn't match in this position
  37.   else
  38.   {
  39.     //We know we're now done matching left to right,
  40.     //so build the substring here of what did match
  41.     //between the two
  42.     $matching = substr($truth, -1 * $different);
  43.  
  44.     //Break out of the for loop, because the left to
  45.     //right matching comparisson has now failed
  46.     break;
  47.   }
  48. }
  49.  
  50. echo 'Different: ' . $different . '<br>';
  51. echo 'Same: ' . $same . '<br>';
  52. echo 'Matching Part: ' . $matching . '<br>';
  53.  
  54. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement