note 64154 added to function.substr
| From: | g8z at yahoo dot com | Date: | Sat, 08 Apr 2006 21:55:31 +0000 |
| Subject: | note 64154 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-107571@lists.php.net to get a copy of this message | ||
<?php
/**
* Gets the common substring from all values of $arr, starting from $offset
* Courtesy of the $5 Script Archive: http://www.tufat.com
*/
function findcommon( $arr, $offset = 0 ) {
$common = FALSE;
$mlen = strlen($arr[0]);
foreach($arr as $str) {
if ( $mlen > strlen( $str ) ) {
$mlen = strlen($str);
}
}
for( $p = $offset + 1; $p < $mlen; $p++ )
{
$compare = substr( $arr[0], $offset, $p - $offset );
$match = TRUE;
foreach( $arr as $str ) {
if ( substr( $str, $offset, $p - $offset ) != $compare ) {
$match = FALSE;
}
}
if ( $match ) {
$common = $compare;
}
}
return $common;
}
$codes = array(
'ABC 123',
'ABC 124',
'ABC 145',
);
echo findcommon( $codes ); // result: "ABC 1"
$codes2 = array(
'ABC 1',
'ABC 123',
'ABC 456',
);
echo findcommon( $codes2 ); // result: "ABC "
$codes3 = array(
'ABC 123',
'ABC 124',
);
echo findcommon( $codes3, 1 ); // result: "BC 12"
$codes4 = array(
'ABC 123',
'AB3 ABC',
);
echo findcommon( $codes4, 2 ); // result: false
?>
----
Server IP: 216.194.113.175
Probable Submitter: 208.54.15.129
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- http://master.php.net/note/edit/64154
Del: integrated -- http://master.php.net/note/delete/64154/integrated
Del: useless -- http://master.php.net/note/delete/64154/useless
Del: bad code -- http://master.php.net/note/delete/64154/bad+code
Del: spam -- http://master.php.net/note/delete/64154/spam
Del: non-english -- http://master.php.net/note/delete/64154/non-english
Del: in docs -- http://master.php.net/note/delete/64154/in+docs
Del: other reasons-- http://master.php.net/note/delete/64154
Reject -- http://master.php.net/note/reject/64154
Search -- http://master.php.net/manage/user-notes.php