Created
April 5, 2013 15:08
-
-
Save havvg/5320031 to your computer and use it in GitHub Desktop.
Calculate the common prefix of two strings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// The smallest length of all strings, limited at 10 characters. | |
$limit = min(strlen($origin), strlen($destination), 10); | |
// Increment $i as long as the characters at its ($i) position match. | |
for ($i = 0; $i < $limit && $origin[$i] === $destination[$i]; $i++); | |
// $i now contains the length of the common prefix |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is my solution: