Recent Posts
None | 4 sec ago
None | 7 sec ago
None | 39 sec ago
None | 58 sec ago
Ruby | 59 sec ago
C++ | 1 min ago
PHP | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Anonymous on the 23rd of Oct 2008 03:24:15 AM Download | Raw | Embed | Report
  1. <?php
  2. function markup($pat,$hay,$out = false) {
  3.   global $groupstyles;
  4.   global $hfieldset;
  5.   global $out;
  6.   $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');
  7.   $hayLen = strlen($hay);
  8.   $matches = true;
  9.   $h = '';
  10.   if (!$out) {
  11.     $matches = preg_match_all($pat, $hay, $out, PREG_OFFSET_CAPTURE|PREG_SET_ORDER);
  12.   }
  13.   if ($matches) {
  14.     $lastOff = 0;
  15.       foreach ($out as $matchArr) { //each set of matches
  16.         $origFull = $matchArr[0][0]; //full pat match
  17.         $full = htmlentities($origFull);
  18.         $fullOff = $matchArr[0][1];
  19.         $fullLen = strlen($origFull);
  20.         unset($matchArr[0]);
  21.         if (!empty($matchArr)) {
  22.           $fullChars = str_split($origFull);
  23.             foreach ($fullChars as $k => $char) {
  24.               $fullChars[$k] = htmlentities($char);
  25.             }
  26.             $i=0;
  27.             $j=0;
  28.             if(!isset($inds)){
  29.               foreach ($matchArr as $ind=>$value) {
  30.                 $j++;
  31.                 $inds[$j]=array();
  32.                 $inds[$j]['name']=$ind;
  33.               }
  34.               $in=1;
  35.               for ($k = 1; $k < count($inds)+1; $k++) {
  36.                 $inds[$k]['color']=$colorarray[$in]!='' ? $colorarray[$in] : $mycol='#CCCCCC';
  37.                 $in++;
  38.                   if(!is_int($inds[$k]['name'])){
  39.                     $inds[$k]['displayname']=$inds[$k+1]['name']." : '".$inds[$k]['name']."'";
  40.                     $inds[$k]['name'].="_".$inds[$k+1]['name'];
  41.                     $inds[$k]['span']="<span class='g".$inds[$k]['name']."'>";
  42.                     $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>&nbsp;';
  43.                     $groupstyles .= "
  44.  .g".$inds[$k]['name']." {background-color:".$inds[$k]['color'].";}
  45. ";
  46.                     $inds[$k+1]['name']=$inds[$k]['name'];
  47.                     $inds[$k+1]['span']='';
  48.                     $inds[$k+1]['color']=$inds[$k]['color'];
  49.                     $k++;
  50.                   } else {
  51.                     $inds[$k]['span']="<span class='g".$inds[$k]['name']."'>";
  52.                     $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>&nbsp;';
  53.                      $groupstyles .= "
  54.  .g".$inds[$k]['name']." {background-color:".$inds[$k]['color'].";}
  55. ";                }
  56.                 }
  57.               }
  58.               foreach ($matchArr as $ind=>$subInfoArr) {
  59.                 $i++;
  60.                 $span = $inds[$i]['span']!='' ? $inds[$i]['span'] : '' ;
  61.                 $sub = $subInfoArr[0];
  62.                 $subLen = strlen($sub);
  63.                 $subOff = $subInfoArr[1] - $fullOff;
  64.                 $endOff = ($subOff + $subLen) - 1;
  65.                 $fullChars[$subOff] = $span.$fullChars[$subOff];
  66.                 $fullChars[$endOff] .= $inds[$i]['span']!='' ? '</span>' : '';
  67.               }
  68.               $full = implode('',$fullChars);
  69.             }
  70.             if ($fullOff == 0) {
  71.               $left = '';
  72.             } else {
  73.               $left = substr($hay, $lastOff, $fullOff-$lastOff);
  74.             }
  75.             $lastOff = $fullOff + $fullLen;
  76.             $h .= htmlentities($left)."<span class='g00'>$full</span>";
  77.           }
  78.           if ($lastOff < $hayLen) {
  79.             $h .= htmlentities(substr($hay, $lastOff));
  80.           }
  81.           return $h;
  82.         } else {
  83.           return false;
  84.      }
  85. }
  86. $matchpattern='/th(?<named1>is( is ))(?<named2>a ((t)e)s)t/';
  87. $sourcetext='testing this is a test testing this is a test testing';
  88. $hmarkup=markup($matchpattern,$sourcetext);
  89. $highlightdiv="<div>HIGHLIGHTED MATCHES:<br><fieldset style='padding:8px;margin:5px;'><legend>CAPTURE GROUPS&nbsp;</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>&nbsp;$hfieldset<pre>$hmarkup</pre></div>";
  90. ?>
  91. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  92. <html>
  93. <head>
  94. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  95. <style type="text/css">
  96.   .g00 {background-color:#F5FF80;}<?php echo $groupstyles; ?>
  97. </style>
  98. <script type="text/javascript">
  99. <!--
  100. function changecolor(theBox)
  101. {
  102.   var obj=document.getElementsByTagName("span");
  103.   for(var i=0,limit=obj.length;i<limit;++i)
  104.   {
  105.     if( -1<obj[i].className.indexOf(theBox.name) )
  106.     {
  107.       if( theBox.checked )
  108.         obj[i].style.backgroundColor=theBox.value;
  109.       else
  110.         obj[i].style.backgroundColor='transparent';
  111.     }
  112.   }
  113. }
  114. //-->
  115. </script>
  116. </head>
  117. <body>
  118. MATCH PATTERN: <pre><?php echo htmlentities($matchpattern); ?></pre><hr>
  119. <?php echo $highlightdiv;?>
  120. </body>
  121. </html>
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: