I have some html menu, the max width is 990px. now i want to print this menu from an array, and make these menu list(<li>
) in one line. remove the padding space or each menu list, for a conserved calculation, how to limit the total words within 200 characters from a foreach? I have no good idea, in my code, group array into string then cut String length, this is not a good idea.
$a = array("item1","item2","item3" ... "item30");//'item' would be change to any other words.
foreach($a as $b){
$c.=$b.'|';
}
function limitString($string, $limit = 200) {
if(strlen($string) < $limit) {return $string;}
$regex = "/(.{1,$limit})\b/";
preg_match($regex, $string, $matches);
return $matches[1];
}
$d = explode('|',limitString($c));
foreach($d as $e){
echo '<li><a href="">'.$e.'</li>';
}
for example, if the 200th character in item20
, it should be broken in item19
, output 19 menu lists in this foreach.