Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //The user input search term
- $search = '90314';
- //The term you're looking to match
- $truth = '90210';
- //How many characters are matching
- $same = 0;
- //How many characters are different (assume all at the start)
- $different = strlen($truth);
- //The matching part of the two strings
- $matching = '';
- //Iterate over each character of the term you're looking to match
- for($i = 0; $i < strlen($truth); $i++)
- {
- //If the search string is long enough to have this
- //character, and the character matches that of the
- //term to be matched, this character is valid
- if(isset($search[$i]) && $search[$i] == $truth[$i])
- {
- //Increment the count of similar characters
- $same++;
- //Decrement the count of different characters
- $different--;
- //Continue to the next character position
- continue;
- }
- //If each the search string is too short, or the
- //character doesn't match in this position
- else
- {
- //We know we're now done matching left to right,
- //so build the substring here of what did match
- //between the two
- $matching = substr($truth, 0, $same);
- //Break out of the for loop, because the left to
- //right matching comparisson has now failed
- break;
- }
- }
- echo 'Different: ' . $different . '<br>';
- echo 'Same: ' . $same . '<br>';
- echo 'Matching Part: ' . $matching . '<br>';
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement