本文章介绍一篇较实用的文章关于php做站内搜索并高亮显示关键字,这个很多朋友直接利用preg_replace来操作,这是正确的哦,不过我觉得用str_replace更快一些哦,至于原因自己查查这两个函数的区别吧。
如何使用php做站内搜索并高亮显示关键字?代码如下:
- <?php
- require_once 'sqlTools.class.php';//封装类,可执行dql、dml语句
- $info=$_POST['info'];
- $sql="select name,password,email from user_500 where name like '%$info%' or password like '%$info%' or email like '%$info%'";
- $sqlTools=new SqlTools();
- $res=$sqlTools->execute_dql($sql);
- while ($row=mysql_fetch_assoc($res)){
- $row['name']=preg_replace("/($info)/i","<b style="color:red">1</b>",$row['name']);
- $row['password']=preg_replace("/($info)/i","<b style="color:red">1</b>",$row['password']);
- $row['email']=preg_replace("/($info)/i","<b style="color:red">1</b>",$row['email']);
- echo $row['name']."–>".$row['password']."–>".$row['email']."<br>";
- }
- ?>
思路分析:将sql语句中包含的%$info%交给DBMS执行的时候,他会查找字段中含有变量$info的值的信息,
%$info—>查找以$info的值结束的信息
$info%—>查找以$info的值开头的信息
通过正则函数preg_replace()将搜索到的关键字高亮显示,比如:
$row['name']=preg_replace("/($info)/i","<b style="color:red">1</b>",$row['name']);
的意思是:通过POST方接收到的值$info替换为加上样式(红色加粗)的结果,并将结果重新赋给$row[‘name’]
如果要搜索多个关键字的话,可以对接收到值$info进行分割,比如$info_more=explode(" ",$info);//这种方式能对以空格隔开的关键字进行分割,再对分割后的结果挨个进行查询,同样,可以使用正则表达式函数进行替换工作,以高亮显示关键字,代码如下:
sqlTools.class.php的源代码:
- <?php
- class SqlTools{
- private $host="localhost";
- private $dbname="test";
- private $dbuser="root";
- private $dbpwd="";
- private $conn;
- public function __construct(){
- $this->conn=mysql_connect($this->host,$this->dbuser,$this->dbpwd);
- if(!$this->conn){
- die("连接数据库失败".mysql_error());
- }
- mysql_select_db($this->dbname,$this->conn) or die("找不到该数据库".mysql_error());
- mysql_query("set names utf8");
- }
- public function execute_dml($sql){
- $bool=mysql_query($sql);
- if ($bool){
- if ($bool>0) {
- return 1;
- }else{
- return 2;
- }
- }else {
- return 0;
- }
- }
- public function execute_dql($sql){
- $res=mysql_query($sql);
- return $res;
- }
- public function close_conn(){
- mysql_close($this->conn);
- }
- }
- ?>
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » php做站内搜索并高亮显示关键字
波比源码 » php做站内搜索并高亮显示关键字
order levaquin for sale levofloxacin 250mg pills
avodart brand flomax 0.2mg generic zofran order
buy aldactone 25mg sale buy valacyclovir 500mg diflucan 200mg sale
ampicillin cost order generic ampicillin erythromycin online order
purchase sildenafil pill buy fildena for sale order methocarbamol online
sildenafil 100mg price order sildenafil 50mg for sale estrace 1mg ca
lamictal 50mg cost tretinoin brand cheap retin
order tadalis 20mg sale diclofenac us voltaren 100mg ca
cialis 10mg pas cher pharmacie en ligne cialis 40mg viagra pour homme
cialis generika sildenafil 100mg kaufen sildenafil 200mg fГјr frauen
order provigil 200mg online cheap oral diamox 250mg acetazolamide oral
purchase doxycycline generic buy vardenafil pills buy lasix 40mg pills
buy altace 10mg for sale cheap irbesartan 150mg buy azelastine for sale
buy terazosin 5mg online order hytrin online cheap oral sulfasalazine 500 mg
order fosamax 70mg without prescription cost famotidine famotidine 40mg uk
benicar online buy order acetazolamide 250mg sale order diamox 250mg online
order isosorbide 20mg online cheap order digoxin 250 mg online cost telmisartan
bupropion 150 mg brand purchase strattera generic quetiapine 50mg tablet
order generic molnupiravir 200 mg buy naproxen online buy prevacid 30mg generic
cialis 10mg cheap amantadine 100 mg over the counter buy amantadine 100 mg
buy dapsone 100 mg pill order adalat 10mg generic buy aceon 4mg generic
generic medroxyprogesterone 5mg microzide brand order periactin online
provigil 100mg generic canadian pharmacy online coupon stromectol tablets for humans
buy generic luvox 50mg fluvoxamine 100mg pills generic glucotrol 10mg
order isotretinoin accutane 40mg pills buy prednisone online
piracetam pills buy viagra without prescription sildenafil 150mg
zithromax 500mg cheap buy generic prednisolone 5mg neurontin 100mg ca
order cialis 40mg for sale order sildenafil 100mg pills viagra without prescription
buy lasix 100mg for sale generic lasix buy plaquenil 200mg online cheap
aralen without prescription order baricitinib 2mg sale buy olumiant 4mg online cheap
sporanox for sale sporanox 100mg ca buy tinidazole 500mg for sale
order linezolid 600mg generic blackjack poker online casino games free
buy omeprazole 20mg pill order prilosec 20mg pills gambling casinos
purchase lopressor pill metoprolol brand vardenafil 10mg without prescription
help with writing a research paper help with assignments uk real online casino
clomiphene 100mg ca albuterol online buy play online casino real money
brand triamcinolone 10mg clarinex order online order desloratadine pill
priligy 30mg us order generic dapoxetine synthroid cheap
tadalafil for women buy cialis 40mg viagra 50mg brand
purchase cialis oral inderal buy clopidogrel online
zyloprim 100mg brand generic zyloprim 300mg zetia us
motilium 10mg pills buy domperidone flexeril 15mg brand
buy generic cozaar 25mg topamax 200mg usa cheap topiramate
order ozobax pill tizanidine 2mg drug ketorolac canada
imitrex order online order avodart 0.5mg pill cost avodart
buy zantac 150mg pills buy zantac pills order celebrex 100mg for sale
flomax 0.2mg canada buy tamsulosin online spironolactone 25mg canada
order tadalafil 20mg buy ampicillin 500mg online cheap order cipro 1000mg sale
order zocor 10mg without prescription buy generic propecia 5mg finasteride 5mg without prescription
buy cialis 20mg sale generic viagra 50mg order viagra 100mg pills
buy sildenafil tablets cost sildenafil buy tadalafil 10mg pill
online blackjack real money cialis 5mg for sale tadalafil brand name
desyrel 100mg cheap trazodone generic aurogra price
sildenafil next day delivery usa buy lamictal generic buy lamotrigine 200mg for sale
slot machines stromectol 12mg tablet provigil 100mg usa
fildena generic purchase budesonide generic budesonide price
generic tretinoin order avana 200mg online cheap buy generic avanafil 200mg
tadacip for sale buy voltaren generic order indomethacin 50mg pills
buy terbinafine 250mg pills buy suprax 200mg online oral trimox
clarithromycin 500mg cheap buy biaxin 250mg buy antivert without prescription
naprosyn 250mg us order prevacid 30mg without prescription lansoprazole 15mg usa
buy pioglitazone 15mg pill purchase viagra pills buy sildenafil 50mg without prescription
tadalafil 20mg oral online casino real money online casino bonus
live dealer blackjack poker online sites real money online blackjack
gambling casino online casino world quick hits free online slots
purchase altace for sale cost altace order etoricoxib 120mg for sale
buy doxycycline 200mg pill buy cleocin 300mg clindamycin tablet
benicar 10mg cost buy generic depakote 500mg order divalproex 500mg sale
buy acetazolamide 250 mg generic diamox 250mg us buy imuran 50mg online
buy digoxin for sale buy digoxin 250mg generic cost molnunat 200mg
cheap coreg 6.25mg purchase elavil online order amitriptyline 10mg sale
alendronate cost order ibuprofen 600mg generic ibuprofen 600mg drug
buy indomethacin online cheap tamsulosin medication order generic cenforce
doxycycline order order generic doxycycline medrol 16mg tablets
tricor cost us pharmacy viagra sildenafil pill
order metformin 500mg generic metformin canada buy generic tamoxifen 10mg
buy provigil 100mg online order promethazine 25mg generic promethazine 25mg sale
deltasone 20mg oral cost amoxil 1000mg amoxicillin 250mg for sale
stromectol 3mg order deltasone 40mg generic prednisone 10mg uk
generic fildena 50mg order propecia 5mg generic buy generic proscar 5mg
buy isotretinoin 20mg online cheap isotretinoin 40mg pill zithromax 500mg pills
purchase zofran pills buy generic amoxicillin 500mg bactrim 480mg for sale
buy albuterol pill cheap levothyroxine without prescription augmentin 625mg brand
modafinil 100mg us metoprolol buy online lopressor over the counter
buy inderal 10mg purchase fluconazole generic buy coreg 25mg sale
order ditropan sale tacrolimus medication order trileptal 600mg online cheap