note 112605 added to ref.strings
| From: | str at maphpia dot com | Date: | Wed, 03 Jul 2013 20:19:26 +0000 |
| Subject: | note 112605 added to ref.strings | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195448@lists.php.net to get a copy of this message | ||
I was looking for a function to find the common substring in 2 different strings. I tried both the
mb_string_intersect and string_intersect functions listed here but didn't work for me. I found
the algorithm at http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Longest_common_substring#PHP
so here I post you the function
<?php
/**
* Finds the matching string between 2 strings
*
* @param string $string1
* @param string $string2
* @param number $minChars
*
* @return NULL|string
*
* @link http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Longest_common_substring#PHP
*/
function string_intersect($string_1, $string_2)
{
$string_1_length = strlen($string_1);
$string_2_length = strlen($string_2);
$return = "";
if ($string_1_length === 0 || $string_2_length === 0) {
// No similarities
return $return;
}
$longest_common_subsequence = array();
// Initialize the CSL array to assume there are no similarities
for ($i = 0; $i < $string_1_length; $i++) {
$longest_common_subsequence[$i] = array();
for ($j = 0; $j < $string_2_length; $j++) {
$longest_common_subsequence[$i][$j] = 0;
}
}
$largest_size = 0;
for ($i = 0; $i < $string_1_length; $i++) {
for ($j = 0; $j < $string_2_length; $j++) {
// Check every combination of characters
if ($string_1[$i] === $string_2[$j]) {
// These are the same in both strings
if ($i === 0 || $j === 0) {
// It's the first character, so it's clearly only 1 character long
$longest_common_subsequence[$i][$j] = 1;
} else {
// It's one character longer than the string from the previous character
$longest_common_subsequence[$i][$j] = $longest_common_subsequence[$i - 1][$j -
1] + 1;
}
if ($longest_common_subsequence[$i][$j] > $largest_size) {
// Remember this as the largest
$largest_size = $longest_common_subsequence[$i][$j];
// Wipe any previous results
$return = "";
// And then fall through to remember this new value
}
if ($longest_common_subsequence[$i][$j] === $largest_size) {
// Remember the largest string(s)
$return = substr($string_1, $i - $largest_size + 1, $largest_size);
}
}
// Else, $CSL should be set to 0, which it was already initialized to
}
}
// Return the list of matches
return $return;
}
----
Server IP: 69.147.83.199
Probable Submitter: 190.148.81.59
----
Manual Page -- http://php.net/manual/en/ref.strings.php
Edit -- https://master.php.net/note/edit/112605
Del: integrated -- https://master.php.net/note/delete/112605/integrated
Del: useless -- https://master.php.net/note/delete/112605/useless
Del: bad code -- https://master.php.net/note/delete/112605/bad+code
Del: spam -- https://master.php.net/note/delete/112605/spam
Del: non-english -- https://master.php.net/note/delete/112605/non-english
Del: in docs -- https://master.php.net/note/delete/112605/in+docs
Del: other reasons-- https://master.php.net/note/delete/112605
Reject -- https://master.php.net/note/reject/112605
Search -- https://master.php.net/manage/user-notes.php