HTML List Slicer

Relates to PHP

Here is a neat little solution I concocted the other day to slice an HTML list. The list is stored in a cached object wrapped around additional mark-up and I wanted to display only a part of the list.

Original mark-up:


<div id="menu">
<h3>Some Links</h3>
<ul>
<li><a href="">Item 1</a></li>
<li><a href="">Item 2</a></li>
<li><a href="">Item 3</a></li>
..
<li><a href="">Item n</a></li>
</ul>
</div>

Desired output:


<div id="menu">
<h3>Some Links</h3>
<ul>
<li><a href="">Item 1</a></li>
<li><a href="">Item 2</a></li>
<li><a href="">Item 3</a></li>
</ul>
</div>

One solution is to create an array of all the list elements extract the required indexes and wrap the rest of the mark-up back around the retrieved set. This could be achieved using <a href="http://www.php.net/preg_replace_callback" title="PHP Doc for preg_replace_callback">preg_replace_callback()</a>, <a href="http://www.php.net/preg_match" title="PHP Doc for preg_match">preg_match()</a> and some array manipulations. However a condensed and far more elegant solution is a single call to <a href="http://www.php.net/preg_replace" title="PHP Doc for preg_replace">preg_replace()</a> making use of the /e modifier and a user-defined function. The /e modifier makes preg_replace treat the replacement parameter as PHP code after the appropriate references substitution is done.


class SS_Cached_Item {
  
  [snip]
  
  function getMenu($length, $offset = 0) {
    ss_fns_reset_range();  
    $str = htmlentities($this->_menu, ENT_QUOTES);      
    $str = preg_replace('#(<li>.+</li>\s+)#e',
                       "ss_fns_in_range('\\1', $offset, $length)",
                       $str);
    return unhtmlentities($str);
  }

}

function ss_fns_in_range($str, $offset = 0, $length = 10) {
  static $i = -1;  
  if ($str === NULL) { $i = -1; return false; }
  return (++$i >= $offset && $i < ($offset + $length)) ?
                                      $str : "";  
}
function ss_fns_reset_range() { ss_fns_in_range(NULL); }

The function ss_fns_in_range utilises a static variable to determine the equivalent index of the current match to the pattern. If the index is within the defined range the pattern is returned intact, otherwise it is replaced by an empty string. The supporting function ss_fns_reset_range will reset the static variable hence allowing multiple occurances of the callback in a single script execution. Note, <a href="http://www.php.net/htmlentities" title="PHP Doc for htmlentities">htmlentities()</a> is applied to the string to ensure special characters are not escaped during the replacement.


// display the first five list items
$obj->getMenu(5);    

 // display four items start at offset 2
$obj->getMenu(4, 2);

This functionality could easily be extended to generate random selections from the menu list.

Posted on Friday, Aug 13, 2004 at 06:01:50.

Comments on HTML List Slicer (0)

Breadcrumbs Trail

[ Home ] -> TW Blog -> Aug 04 -> HTML List Slicer
Site Map

The Severn Solutions website achieves the following standards:

[ XHTML 1.0 ] [ CSS 2 ] [ WAI AA ] [ Bobby AA ]

Page compiled in 0.010 seconds