loading

parsefold() - folded nesting

parsefold() lets you display nested data in a folding block format. See http://defproc.co.uk/fold/test.php for a demonstration. It accepts three arguments: the data itself, which strings signal the start and end of a block and (boolean) whether or not to keep those 'signals' intact in the function's output.

An adaptation idea might be to allow regular expression instead of start/end strings to identify blocks. <script language='javascript'>

function toggle(id){
 a = document.getElementById("a_"+id);
 if (a.innerHTML == '+'){
  document.getElementById("fold_"+id).style.display="block";
  a.innerHTML="-";
 } else {
  document.getElementById("fold_"+id).style.display="none";
  a.innerHTML='+';
 }
}

</script>

<?php

function parsefold($f,$start="<block>",$end="</block>",$show=true){
 
$nest = array();
 
$l strlen($f);
 
$id 0;
 
$o '';
 
$skip 0;
 for (
$p 0$p $l$p++){
  if (
substr($f,$p,strlen($start)) == $start){
   
array_push($nest,$id);
   
$o .= $show $start '';
   
$o .= "<font color='#0066ff'><b>[<a style='cursor: crosshair;' id='a_$id' onclick='toggle($id);'>+</a>]</b></font>";
   
$o .= "<div style='margin-left: " . (sizeof($nest)*16) . "px; display: none;' id='fold_$id'>";
   
$id++;
   
$skip += (strlen($start)-1);
  }
  elseif (
substr($f,$p,strlen($end)) == $end){
   
$cid array_pop($nest);
   
$o .= "</div>";
   
$o .= $show $end '';
   
$skip += (strlen($end)-1);
  }
  else {
   if (
$skip 0){
    
$skip--;
   } else {
    
$o .= substr($f,$p,1);
   }
  }
 }
 return 
$o;
}

?>
(unrated) | by defproc on March 24th, 2008

An account is required to post comments and adaptations

challengebee.org | Powered by: Apache / PHP / MySQL | Made in Ninjapad NX | © defproc.co.uk, 2007_