1

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.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
cj333
  • 2,547
  • 20
  • 67
  • 110
  • 4
    why use a regex when you could just `substr()`? you don't need a bazooka when a spitball will do just as well. – Marc B Apr 26 '13 at 14:32
  • @Marc B, so this is not a good idea, any easy way? – cj333 Apr 26 '13 at 14:34
  • 1
    suck out 200 chars with substr, then scan backwards from the end of the string to the first separator, slice off the dangling bit at the end, and you'd end up with a non-split sub string. – Marc B Apr 26 '13 at 14:35
  • Do you want to limit each word equally ? Or do you want to remove the rest of the menus when you reach 200 char ? – HamZa Apr 26 '13 at 14:40
  • @HamZa DzCyberDeV, remove the rest of the menus when i reach 200 chars – cj333 Apr 26 '13 at 14:42

3 Answers3

1

You could use substr() as other comments say. To preserve words split, have look at this thread.

Anyway, there's a better way to play with string physical length, but it involves JavaScript.

All you have to do is to create invisible div containing string you want to measure, and then getting it's width and height. Why would you do that? Of course because each letter has different width, also various browsers on various OSes display fonts in little bit other way.

Community
  • 1
  • 1
ex3v
  • 3,518
  • 4
  • 33
  • 55
1

Try the following:

$array = array("item1","item2","item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10", "item11", "item12", "item13", "item14", "item15", "item16", "item17", "item18", "item19", "item20", "item21", "item22", "item23", "item24", "item25", "item26", "item27", "item28", "item29", "item30", "item31", "item32", "item33", "item34", "item35", "item36", "item37", "item38", "item39", "item40", "item41", "item42", "item43", "item44", "item45", "item46", "item47", "item48", "item49", "item50", "item51", "item52", "item53", "item54", "item55", "item56", "item57", "item58", "item59", "item60", "item61", "item62", "item63", "item64", "item65", "item66", "item67", "item68", "item69", "item70", "item71", "item72", "item73", "item74", "item75", "item76", "item77", "item78", "item79", "item80", "item81", "item82", "item83", "item84", "item85", "item86", "item87", "item88", "item89", "item90", "item91", "item92", "item93", "item94", "item95", "item96", "item97", "item98", "item99", "item100");
$max = 200;


$length = 0;
$menu_items = array();
foreach($array as $value){
    $length += strlen($value);
    if($length <= $max){
        $menu_items[] = $value;
    }else{
        break;
    }
}

// Outputing the menu:
echo '<ul><li>'. implode('</li><li>', $menu_items) .'</li></ul>';

Online demo.

HamZa
  • 14,671
  • 11
  • 54
  • 75
0

You can check it at this part:

foreach($a as $b){
if(strlen($c)<200)  $c.=$b.'|'; else return;
}
Kuzgun
  • 4,649
  • 4
  • 34
  • 48