With Php Custom Search, full-featured text search engine library written entirely in Php,javascript and xml. It is a technology suitable for nearly any application that requires full-text search.
step-1 First you create index.html page and adding some javascript code for using functionality livesearch.php page
<html> | |
<head> | |
<script> | |
function showResult(str) { | |
if (str.length==0) { | |
document.getElementById("livesearch").innerHTML=""; | |
document.getElementById("livesearch").style.border="0px"; | |
return; | |
} | |
if (window.XMLHttpRequest) { | |
// code for IE7+, Firefox, Chrome, Opera, Safari | |
xmlhttp=new XMLHttpRequest(); | |
} else { // code for IE6, IE5 | |
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); | |
} | |
xmlhttp.onreadystatechange=function() { | |
if (this.readyState==4 && this.status==200) { | |
document.getElementById("livesearch").innerHTML=this.responseText; | |
document.getElementById("livesearch").style.border="1px solid #A5ACB2"; | |
} | |
} | |
xmlhttp.open("GET","livesearch.php?q="+str,true); | |
xmlhttp.send(); | |
} | |
</script> | |
</head> | |
<body> | |
<form> | |
<input type="text" size="30" onkeyup="showResult(this.value)"> | |
<div id="livesearch"></div> | |
</form> | |
</body> | |
</html> |
step-2 Then you create livesearch.php page
<?php | |
$xmlDoc = new DOMDocument(); | |
$xmlDoc->load("links.xml"); | |
$x=$xmlDoc->getElementsByTagName('link'); | |
$q=$_GET["q"]; | |
if(strlen($q)>0) | |
{ | |
$hint=""; | |
for($i=0;$i<($x->length);$i++) | |
{ | |
$y=$x->item($i)->getElementsByTagName('title'); | |
$z=$x->item($i)->getElementsByTagName('url'); | |
if($y->item(0)->nodeType==1) | |
{ | |
if(stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) | |
{ | |
if($hint="") | |
{ | |
$hint="<a href='" . | |
$z->item(0)->childNodes->item(0)->nodeValue. | |
"' target='_blank'>" . | |
$y->item(0)->childNodes->item(0)->nodeValue."</a>"; | |
} | |
else | |
{ | |
$hint=$hint . "<br/><a href='" . | |
$z->item(0)->childNodes->item(0)->nodeValue . | |
"' target='_blank'>". | |
$y->item(0)->childNodes->item(0)->nodeValue. "</a>"; | |
} | |
} | |
} | |
} | |
} | |
if($hint==""){ | |
$response = "No results found"; | |
} | |
else | |
{ | |
$response = $hint; | |
} | |
echo $response; | |
?> |
step-3 Then you create links.xml file links.xml is used for linking with search data.
<pages> | |
<link> | |
<title>HTML br tag</title> | |
<url>https://www.w3schools.com/tags/tag_br.asp</url> | |
</link> | |
<link> | |
<title>CSS background Property</title> | |
<url> | |
https://www.w3schools.com/cssref/css3_pr_background.asp | |
</url> | |
</link> | |
<link> | |
<title>CSS border Property</title> | |
<url>https://www.w3schools.com/cssref/pr_border.asp</url> | |
</link> | |
<link> | |
<title>JavaScript Date Object</title> | |
<url>https://www.w3schools.com/jsref/jsref_obj_date.asp</url> | |
</link> | |
<link> | |
<title>JavaScript Array Object</title> | |
<url> | |
https://www.w3schools.com/jsref/jsref_obj_array.asp | |
</url> | |
</link> | |
</pages> |
Iβm a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND