Site Highlight Search and keyword highlighting php/javascript code

Source: Internet
Author: User
Tags php code isearch

PHP code

The code is as follows Copy Code

<?php
Class Search extends scurd{
/* constructor, initializing the connection, using the same method as the parent class * *
function __construct ($HOST, $DB _user, $PASSWD, $DB) {
Parent::__construct ($HOST, $DB _user, $PASSWD, $DB);
}
/* destructor, close connection * *
function __destruct () {
Parent::__destruct ();
}
/**
* Public method, search string and highlight text, return a two-dimensional array result set
* @parameter $table (type of string, required), fill in the table of the required query
* @parameter $fieldName (type of string, required), fill in the name of the field you want to search
* @parameter $strSearch (String type, required), fill in the required search string
* @parameter $hlColor (type of string, optional), fill in the color of the highlighted text, the default is red
**/
function ISearch ($table, $fieldName, $strSearch, $hlColor = ' red ') {
$arrSearch = Explode (", $strSearch);
foreach ($arrSearch as $value) {
$conditions [] = "$fieldName like '% $value% '";
}
$condition = Is_array ($conditions)? Implode (' OR ', $conditions): $conditons [0];
$ARRSTR = Parent::r ($table, Array (' * '), $condition);
if ($ARRSTR) {
for ($i = 0; $i < count ($arrStr); $i + +) {
foreach ($arrSearch as $key => $sValue) {
$ARRSTR [$i] [$fieldName] = $this->hlstr ($arrSearch [$key], $arrStr [$i] [$fieldName], $hlColor);
}
}
}
return $arrStr;
}
/**
* Private method, highlighted text, returns the highlighted string
* @parameter $needle (type of string, required), fill in the string you want to find
* @parameter $haystack (String type, required), fill in the searched string
* @parameter $color (String type, required), fill in the color of the highlighted text
**/
Private Function Hlstr ($needle, $haystack, $color) {
$str = Str_ireplace ("$needle", "<font style= ' color: $color; ' > $needle </font> ", $haystack);
return $str;
}
}

Using the sample
if (Isset ($_get[' Txtsearch ')) {//To determine whether the form is submitted
$str = $_get[' Txtsearch ']; The search string assignment to $str
$s = new search (' localhost ', ' root ', ' ", ' test '); Instantiate a search class
$results = $s->isearch (' test ', ' message ', $STR); Assign the result set to $results
if ($results) {//To determine whether the result set is empty
foreach ($results as $key => $value) {//Traverse result set
Echo ' first '. ($key + 1). ' Record:<br> ';
Echo ' ID: '. $value [' id ']. ' ' . ' Name: '. $value [' name '];
Echo ' <br> message:<br> '. $value [' message ']. ' <p> ';
}
}
else{
Echo ' no record ';
}
}
?>
<!--write a form submit to this page-->
<form action= "" method= "get" >
<input name= "Txtsearch" type= "text"/>
<input name= "btnsearch" type= "submit" value= "Search"/>
</form>

For dynamic pages

In general we will be the key word preprocessing, so that it highlighted, then static page how to achieve the display of highlighting? Here we rely on JavaScript to simply implement this function

The code is as follows Copy Code

<style type= "Text/css" >
. Highlight {
Background-color: #ffff00;
color:red;
}
</style>

Implement parameter acquisition

And the important function of highlighting

The code is as follows Copy Code

<script language= "JavaScript" type= "Text/javascript" >
<!--
function Gethtmlpara (p)
{
var url=location.search.substring (1);
var tempstr=p+ "=";
if (Url.indexof (TEMPSTR) ==-1) return null;
if (Url.indexof ("&")!=-1)
{
var a=url.split ("&");
var i=0;
for (i=0;i<a.length;i++)
{
if (A.indexof (TEMPSTR)!=-1) return a.substring (tempstr.length);
}
}
Else
{
Return url.substring (tempstr.length);
}
}
function Htmlhighlight (obj)
{
var rea = Gethtmlpara (' highlight ');
if (rea!=null && rea!= ')
{
var Arrayofrea = rea.split ("+");
var el = document.getElementById (obj);
var thisstr = el.innerhtml;
var bb = ' <span class= ' highlight ' >$1</span> ';
for (var i=0; i<arrayofrea.length; i++)
{
if (arrayofrea!=null && arrayofrea!= ')
{
Re = new RegExp ("+arrayofrea+"), "GI");
THISSTR = Thisstr.replace (Re, BB);
}
}
el.innerhtml = Thisstr;
}
}
-->
</script>

Calling method: When a static page transmits a highlight parameter, the corresponding ID region keyword is highlighted. Page address such as: http://www.xx.com/test.html?highlight= keyword

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.