<?php
function markup($pat,$hay,$out = false) {
global $groupstyles;
global $hfieldset;
global $out;
$colorarray=array ('#F5FF80','#99FF80','#80FFFF','#809FFF','#FF80B8','#FFB380','#EBFFE6','#E6FFFF','#FF9900','#DABFFF','#FFF0E6','#FFF280','#FFCCBF','#BFCFFF','#FF9980','#FAFFE6','#31FFC1','#FF89FF','#FFC800','#FF6464','#22A2A2','#DDFF08','#FFCCFF','#FFCCCC','#FFCC99','#FFCC66','#FFCECE','#A095EE','#C9EAF3','#CFFEF0','#CAFFD8','#B4D1B6','#B3FF99','#DFDF00','#EDEF85','#2DC800');
$matches = true;
$h = '';
if (!$out) {
$matches = preg_match_all($pat, $hay, $out, PREG_OFFSET_CAPTURE
|PREG_SET_ORDER
);
}
if ($matches) {
$lastOff = 0;
foreach ($out as $matchArr) { //each set of matches
$origFull = $matchArr[0][0]; //full pat match
$fullOff = $matchArr[0][1];
foreach ($fullChars as $k => $char) {
}
$i=0;
$j=0;
foreach ($matchArr as $ind=>$value) {
$j++;
$inds[$j]['name']=$ind;
}
$in=1;
for ($k = 1; $k < count($inds)+1; $k++) {
$inds[$k]['color']=$colorarray[$in]!='' ? $colorarray[$in] : $mycol='#CCCCCC';
$in++;
if(!is_int($inds[$k]['name'])){
$inds[$k]['displayname']=$inds[$k+1]['name']." : '".$inds[$k]['name']."'";
$inds[$k]['name'].="_".$inds[$k+1]['name'];
$inds[$k]['span']="<span class='g".$inds[$k]['name']."'>";
$hfieldset.='<fieldset style="width:10px;padding:6px;margin:3px;background-color:'.$inds[$k]['color'].';display:inline;"><legend>'.$inds[$k]['displayname'].'</legend><input type="checkbox" value="'.$inds[$k]['color'].'" onclick="changecolor(this);" checked name="'.$inds[$k]['name'].'"></fieldset> ';
$groupstyles .= "
.g".$inds[$k]['name']." {background-color:".$inds[$k]['color'].";}
";
$inds[$k+1]['name']=$inds[$k]['name'];
$inds[$k+1]['span']='';
$inds[$k+1]['color']=$inds[$k]['color'];
$k++;
} else {
$inds[$k]['span']="<span class='g".$inds[$k]['name']."'>";
$hfieldset.='<fieldset style="width:10px;padding:6px;margin:3px;background-color:'.$inds[$k]['color'].';display:inline;"><legend>'.$inds[$k]['name'].'</legend><input type="checkbox" value="'.$inds[$k]['color'].'" onclick="changecolor(this);" checked name="g'.$inds[$k]['name'].'"></fieldset> ';
$groupstyles .= "
.g".$inds[$k]['name']." {background-color:".$inds[$k]['color'].";}
"; }
}
}
foreach ($matchArr as $ind=>$subInfoArr) {
$i++;
$span = $inds[$i]['span']!='' ? $inds[$i]['span'] : '' ;
$sub = $subInfoArr[0];
$subOff = $subInfoArr[1] - $fullOff;
$endOff = ($subOff + $subLen) - 1;
$fullChars[$subOff] = $span.$fullChars[$subOff];
$fullChars[$endOff] .= $inds[$i]['span']!='' ? '</span>' : '';
}
}
if ($fullOff == 0) {
$left = '';
} else {
$left = substr($hay, $lastOff, $fullOff-$lastOff);
}
$lastOff = $fullOff + $fullLen;
}
if ($lastOff < $hayLen) {
}
return $h;
} else {
return false;
}
}
$matchpattern='/th(?<named1>is( is ))(?<named2>a ((t)e)s)t/';
$sourcetext='testing this is a test testing this is a test testing';
$hmarkup=markup($matchpattern,$sourcetext);
$highlightdiv="<div>HIGHLIGHTED MATCHES:<br><fieldset style='padding:8px;margin:5px;'><legend>CAPTURE GROUPS </span></legend><fieldset style='width:10px;padding:6px;margin:3px;background-color:#F5FF80;display:inline;'><legend>0</legend><input type='checkbox' value='#F5FF80' onclick='changecolor(this);' checked name='g00'></fieldset> $hfieldset<pre>$hmarkup</pre></div>";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.g00 {background-color:#F5FF80;}<?php echo $groupstyles; ?>
</style>
<script type="text/javascript">
<!--
function changecolor(theBox)
{
var obj=document.getElementsByTagName("span");
for(var i=0,limit=obj.length;i<limit;++i)
{
if( -1<obj[i].className.indexOf(theBox.name) )
{
if( theBox.checked )
obj[i].style.backgroundColor=theBox.value;
else
obj[i].style.backgroundColor='transparent';
}
}
}
//-->
</script>
</head>
<body>
MATCH PATTERN: <pre>
<?php echo htmlentities($matchpattern); ?></pre><hr>
<?php echo $highlightdiv;?>
</body>
</html>