<?php
$input = '1234567';
$array = ['1', '12', '123', '1234', '456', '56', '7', '3456', '234567'];
function get_best_prefix($input, $array){
$best = '';
$sizeBest = 0;
foreach ($array as $data) {
if (strpos($input, $data) === 0) {
$current = strlen($data);
if($sizeBest < $current){
$sizeBest = $current;
$best = $data;
}
}
}
return $best;
}
echo get_best_prefix($input, $array);
preferences:
51.87 ms | 402 KiB | 5 Q