, !
:
$found=false;
for($x=1,$max=sizeof($data); $x<=$max; ++$x){
if(array_slice($data,0,$x)===array_slice($data,-$x)){
$found=true;
}elseif($found){
--$x;
break;
}
}
var_export($found?array_slice($data,0,$x):"No match");
:
$data=['a','b','c','e','r','t','x','s','b','a','b','c']; // ['a','b','c']
$data=['n','o','p','e']; // No Match
$data=['r','a','c','e','c','a','r']; // ['r']
$data=['a','a','b','a','a']; // ['a','a']
:
, . , , ( ).
array_slice() . $x , array_slice() , .
$max "" . "", $max=floor(sizeof($data)/2)
, , , , .
...
Palindromic Matching - you can easily customize my method above to match mirror sequences by adding array_reverse().
Method:
$found=false;
for($x=1,$max=sizeof($data); $x<=$max; ++$x){
if(array_slice($data,0,$x)===array_reverse(array_slice($data,-$x))){
$found=true;
}elseif($found){
--$x;
break;
}
}
var_export($found?array_slice($data,0,$x):"No match");
Inputs and outputs:
$data=['a','b','c','e','r','t','x','s','b','a','b','c']; // No Match
$data=['n','o','p','e']; // No Match
$data=['r','a','c','e','c','a','r']; // ['r','a','c','e','c','a','r']
$data=['a','a','b','a','a']; // ['a','a','b','a','a']