Break out highlighting
Break out highlighting

--- a/include/display.git_search.php
+++ b/include/display.git_search.php
@@ -8,6 +8,7 @@
  */
 
 require_once('defs.constants.php');
+require_once('util.highlight.php');
 require_once('gitutil.git_read_commit.php');
 require_once('gitutil.git_rev_list.php');
 
@@ -91,28 +92,9 @@
 			$tpl->assign("committree",$co2['tree']);
 			$matches = array();
 			foreach ($co2['comment'] as $comline) {
-				if (eregi("(.*)(" . quotemeta($search) . ")(.*)",$comline,$regs)) {
-					$linelen = strlen($regs[0]);
-					if ($linelen > GITPHP_TRIM_LENGTH) {
-						$matchlen = strlen($regs[2]);
-						$remain = floor((GITPHP_TRIM_LENGTH - $matchlen) / 2);
-						$leftlen = strlen($regs[1]);
-						$rightlen = strlen($regs[3]);
-						if ($leftlen > $remain) {
-							$leftremain = $remain;
-							if ($rightlen < $remain)
-								$leftremain += ($remain - $rightlen);
-							$regs[1] = "..." . substr($regs[1], ($leftlen - ($leftremain - 3)));
-						}
-						if ($rightlen > $remain) {
-							$rightremain = $remain;
-							if ($leftlen < $remain)
-								$rightremain += ($remain - $leftlen);
-							$regs[3] = substr($regs[3],0,$rightremain-3) . "...";
-						}
-					}
-					$matches[] = $regs[1] . "<span class=\"searchmatch\">" . $regs[2] . "</span>" . $regs[3];
-				}
+				$hl = highlight($comline, $search, "searchmatch", GITPHP_TRIM_LENGTH);
+				if ($hl && (strlen($hl) > 0))
+					$matches[] = $hl;
 			}
 			$tpl->assign("matches",$matches);
 			$tpl->display("search_item.tpl");

--- /dev/null
+++ b/include/util.highlight.php
@@ -1,1 +1,45 @@
+<?php
+/*
+ *  util.highlight.php
+ *  gitphp: A PHP git repository browser
+ *  Component: Utility - highlight a string
+ *
+ *  Copyright (C) 2008 Christopher Han <xiphux@gmail.com>
+ */
 
+function highlight($haystack, $needle, $highlightclass, $trimlen = NULL)
+{
+	if (eregi("(.*)(" . quotemeta($needle) . ")(.*)",$haystack,$regs)) {
+		if (isset($trimlen) && ($trimlen > 0)) {
+			$linelen = strlen($regs[0]);
+			if ($linelen > $trimlen) {
+				$matchlen = strlen($regs[2]);
+				$remain = floor(($trimlen - $matchlen) / 2);
+				$leftlen = strlen($regs[1]);
+				$rightlen = strlen($regs[3]);
+				if ($leftlen > $remain) {
+					$leftremain = $remain;
+					if ($rightlen < $remain)
+						$leftremain += ($remain - $rightlen);
+					$regs[1] = "..." . substr($regs[1], ($leftlen - ($leftremain - 3)));
+				}
+				if ($rightlen > $remain) {
+					$rightremain = $remain;
+					if ($leftlen < $remain)
+						$rightremain += ($remain - $leftlen);
+					$regs[3] = substr($regs[3],0,$rightremain-3) . "...";
+				}
+			}
+		}
+		$ret = $regs[1] . "<span";
+		if ($highlightclass)
+			$ret .= " class=\"" . $highlightclass . "\"";
+		$ret .= ">" . $regs[2] . "</span>" . $regs[3];
+		return $ret;
+	}
+
+	return false;
+}
+
+?>
+

comments