note 94059 added to ref.strings
| From: | Peter-thepete-dePijd at osu1 dot php dot net | Date: | Wed, 14 Oct 2009 12:01:41 +0000 |
| Subject: | note 94059 added to ref.strings | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161726@lists.php.net to get a copy of this message | ||
get the intersection of two strings; may not have optimal performance
<?php
function mb_string_intersect($string1, $string2, $minChars = 5)
{
assert('$minChars > 1');
$string1 = trim($string1);
$string2 = trim($string2);
$length1 = mb_strlen($string1);
$length2 = mb_strlen($string2);
if ($length1 > $length2) {
// swap variables, shortest first
$string3 = $string1;
$string1 = $string2;
$string2 = $string3;
$length3 = $length1;
$length1 = $length2;
$length2 = $length3;
unset($string3, $length3);
}
if ($length2 > 255) {
return null; // to much calculation required
}
for ($l = $length1; $l >= $minChars; --$l) { // length
for ($i = 0, $ix = $length1 - $l; $i <= $ix; ++$i) { // index
$substring1 = mb_substr($string1, $i, $l);
$found = mb_strpos($string2, $substring1);
if ($found !== false) {
return trim(mb_substr($string2, $found, mb_strlen($substring1)));
}
}
}
return null;
}
echo mb_string_intersect("hello spencer", "hey spencer how are you?"); // output
"spencer"
?>
----
Server IP: 192.41.40.227
Probable Submitter: 89.244.130.51
----
Manual Page -- http://www.php.net/manual/en/ref.strings.php
Edit -- https://master.php.net/note/edit/94059
Del: integrated -- https://master.php.net/note/delete/94059/integrated
Del: useless -- https://master.php.net/note/delete/94059/useless
Del: bad code -- https://master.php.net/note/delete/94059/bad+code
Del: spam -- https://master.php.net/note/delete/94059/spam
Del: non-english -- https://master.php.net/note/delete/94059/non-english
Del: in docs -- https://master.php.net/note/delete/94059/in+docs
Del: other reasons-- https://master.php.net/note/delete/94059
Reject -- https://master.php.net/note/reject/94059
Search -- https://master.php.net/manage/user-notes.php