插件25:突出显示搜索结果

<?php // Plug-in 25: Query Highlight

// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$text = "To be or not to be, that is the question; " .
        "whether 'tis nobler in the mind to suffer " .
        "the slings and arrows of outrageous fortune, " .
        "or to take arms against a sea of troubles, " .
        "and by opposing, end them. To die - to sleep, " .
        "no more; and by a sleep to say we end " .
        "the heart-ache and the thousand natural shocks " .
        "that flesh is heir to - 'tis a consummation " .
        "devoutly to be wish'd.";

echo    "<a href=\"" . $_SERVER['PHP_SELF'] .
        "?q=" . rawurlencode("question of sleep") .
        "\">Click twice to test</a><br />";
echo    PIPHP_QueryHighlight($text, "b");

function PIPHP_QueryHighlight($text, $highlight)
{
   // Plug-in 25: Query Highlight
   //
   // This plug-in text to be highlighted if this page was
   // reached as a result of a search engine query. It takes
   // these arguments:
   //
   //    $text:      The text to display
   //    $highlight: The highlight to use:
   //                "b", "i" or "u": Bold, italic or
   //                underlined

	$refer = getenv('HTTP_REFERER');
	$parse = parse_url($refer);
   
	if ($refer == "") return $text;
	elseif (!isset($parse['query'])) return $text;

	$queries = explode('&', $parse['query']);

	foreach($queries as $query)
   {
      list($key, $value) = explode('=', $query);
      
      if ($key == "q" || $key == "p")
      {
         $matches = explode(' ', preg_replace('/[^\w ]/', '',
            urldecode($value)));
         return PIPHP_WordSelector($text, $matches, $highlight);
      }
   }
}

// The function below is repeated here to ensure it is
// available to the main function which makes use of it

function PIPHP_WordSelector($text, $matches, $replace)
{
   // Plug-in 5: Word Selector
   // This plug-in takes a string variable containing any
   // text and then matches and selects words within the
   // text, highlighting or removing them.
   // The arguments are:
   //    $text:    Text to be modified
   //    $matches: Array of words to match
   //    $replace: String to replace matches with, or if
   //              string is one of these the text is hilighted:
   //              "u", "b", "i" = underline, bold or italic.

   foreach($matches as $match)
   {
      switch($replace)
      {
         case "u":
         case "b":
         case "i":
            $text = preg_replace("/([^\w]+)($match)([^\w]+)/i",
               "$1<$replace>$2</$replace>$3", $text);
            break;

         default:
            $text = preg_replace("/([^\w]+)$match([^\w]+)/i",
               "$1$replace$2", $text);
            break;
      }
   }

   return $text;
}

?>

插件说明

本插件接受一个文本和搜索关键字的突出显示类型。它需要以参数如下:

$text 被突出显示的文本

$highlight 被突出显示的类型,取b、i或u值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值