note 26599 added to ref.strings

From: Date: Wed, 06 Nov 2002 11:32:03 +0000
Subject: note 26599 added to ref.strings
Groups: php.notes 
Request: Send a blank email to php-notes+get-39030@lists.php.net to get a copy of this message
ENG: I was searching for a way to calculate the intersection of strings, so here is my solution (there may be better ways, but it works fine for me): GER: Ich suchte einen Weg um die Schnittmenge zwischen Strings herauszufinden, und folgende Lösung habe ich gefunden (Es mag bessere Wege geben, dieser war für mich aber ganz o.k.). <?php function str_intersect($str,$cmp) { if ( !is_string($str) || !is_string($cmp) ) { return false; } $str = trim($str); $cmp = trim($cmp); if ( strlen($str) == 0 || strlen($cmp) == 0 ) { return false; } $found = Array(); for ( $cnta=0; $cnta<=strlen($cmp); $cnta++ ) { for ( $cntb=0; $cntb<=strlen($cmp); $cntb++ ) { $tmpa = trim(substr($cmp,$cnta,$cntb)); if ( strlen($tmpa) > 0 ) { $tmpb = strrev($tmpa); if ( stristr($str,$tmpa) ) { $found[] = $tmpa; } if ( $tmpa != $tmpb ) { if ( stristr($str,$tmpb) ) { $found[] = $tmpb; } } } } } if ( $found === Array() ) { return false; } usort ($found, "ssort"); $found = current($found); if ( strlen($found) <= 1 ) { $found = false; } return $found; } function ssort ($a, $b) { if ($a == $b) return 0; return (strlen($a) > strlen($b) ) ? -1 : 1; } ?> <?php print str_intersect("Gute Morgen Deutschland","Deutschland ist arbeitslos!"); ?> prints the string "Deutschland" Hans Kuhlen -- http://www.php.net/manual/en/ref.strings.php http://master.php.net/manage/user-notes.php?action=edit+26599 http://master.php.net/manage/user-notes.php?action=delete+26599 http://master.php.net/manage/user-notes.php?action=reject+26599

« previous php.notes (#39030) next »