看PHP如何实现多关键字加亮_PHP教程

WBOY
Freigeben: 2016-07-20 10:58:13
Original
757 Leute haben es durchsucht

项目结构:

\

开始搜索:   这里搜索关键字("大""这")

\

搜索结果:  高亮显示

\

项目所需数据库结构:

\

实现代码:

conn.php

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li class="alt">
<span class="vars">$conn</span><span> = @ mysql_connect(</span><span class="string">"localhost"</span><span>, </span><span class="string">"root"</span><span>, </span><span class="string">""</span><span>) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span>(</span><span class="string">"数据库链接错误"</span><span>);  </span>
</li>
<li>
<span>mysql_select_db(</span><span class="string">"form"</span><span>, </span><span class="vars">$conn</span><span>);  </span>
</li>
<li class="alt">
<span>mysql_query(</span><span class="string">"set names 'gbk'"</span><span>);   </span>
</li>
<li><span>?> </span></li>
</ol>
Nach dem Login kopieren

searchAndDisplayWithColor.php

  
<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li>
<span class="keyword">include</span><span> </span><span class="string">'conn.php'</span><span>;  </span>
</li>
<li class="alt"><span>?>  </span></li>
<li><span><table>
<span class="string">"center"</span><span>>  </span><li class="alt"><span>     <form>
<span class="string">""</span><span> method=</span><span class="string">"get"</span><span>>  </span>
</form></span></li>
<li><span>     <tr>  <li class="alt">
<span>         </span><td>关键字:<input><span class="string">"text"</span><span> name=</span><span class="string">"keyWord"</span><span> />  </span><li><span>         <input><span class="string">"submit"</span><span> value=</span><span class="string">"搜索"</span><span> /></span></span></li>
</td>  </li>
<li class="alt"><span>     </span></li>
</tr>  </span></li>
<li><span>       </span></li>
<li class="alt"><span> </span></li>
</table>  </span></li>
<li><span>   </span></li>
<li class="alt"><span> <table>
<span class="string">"0"</span><span> align=</span><span class="string">"center"</span><span> cellpadding=</span><span class="string">"5"</span><span> </span><li>
<span>     cellspacing=</span><span class="string">"1"</span><span> bgcolor=</span><span class="string">"#add3ef"</span><span>>  </span>
</li>
<li class="alt"><span>     <?php  </span></span></li>
<li>
<span>     </span><span class="comment">//关键字不为空的时候才执行相关搜索 </span><span> </span>
</li>
<li class="alt">
<span>     </span><span class="keyword">if</span><span>(</span><span class="vars">$_GET</span><span>[</span><span class="string">'keyWord'</span><span>]){  </span>
</li>
<li>
<span>     </span><span class="comment">//用空格符把关键字分割开 </span><span> </span>
</li>
<li class="alt">
<span>     </span><span class="vars">$key</span><span>=</span><span class="func">explode</span><span>(</span><span class="string">' '</span><span>, </span><span class="vars">$_GET</span><span>[keyWord]);  </span>
</li>
<li>
<span>     </span><span class="vars">$sql</span><span>=</span><span class="string">"select * from message where title like '$key[0]' or title like '$key[1]' or content like '$key[0]' or content like '%$key[1]%'"</span><span>;  </span>
</li>
<li class="alt">
<span>     </span><span class="vars">$query</span><span>=mysql_query(</span><span class="vars">$sql</span><span>);  </span>
</li>
<li>
<span>     </span><span class="keyword">while</span><span> (</span><span class="vars">$row</span><span>=mysql_fetch_array(</span><span class="vars">$query</span><span>)){  </span>
</li>
<li class="alt">
<span>         </span><span class="comment">//替换关键字,并且把关键字高亮显示 </span><span> </span>
</li>
<li>
<span>         </span><span class="vars">$row</span><span>[title]=preg_replace(</span><span class="string">"/$key[0]/i"</span><span>, </span><span class="string">"<font><b>$key[0]</b></font>"</span><span>, </span><span class="vars">$row</span><span>[title]);  </span>
</li>
<li class="alt">
<span>         </span><span class="vars">$row</span><span>[title]=preg_replace(</span><span class="string">"/$key[0]/i"</span><span>, </span><span class="string">"<font><b>$key[1]</b></font>"</span><span>, </span><span class="vars">$row</span><span>[title]);  </span>
</li>
<li>
<span>         </span><span class="vars">$row</span><span>[content]=preg_replace(</span><span class="string">"/$key[0]/i"</span><span>, </span><span class="string">"<font><b>$key[0]</b></font>"</span><span>, </span><span class="vars">$row</span><span>[content]);  </span>
</li>
<li class="alt">
<span>         </span><span class="vars">$row</span><span>[content]=preg_replace(</span><span class="string">"/$key[1]/i"</span><span>, </span><span class="string">"<font><b>$key[1]</b></font>"</span><span>, </span><span class="vars">$row</span><span>[content]);  </span>
</li>
<li><span>         ?>  </span></li>
<li class="alt"><span>   </span></li>
<li><span>     <tr>
<span class="string">"#eff3ff"</span><span>>  </span><li class="alt">
<span>         </span><td>标题:<font><span class="string">"black"</span><span>>=</span><span class="vars">$row</span><span>[title]?></span></font> 用户:<font><span class="string">"black"</span><span>>=</span><span class="vars">$row</span><span>[user] ?></span></font>  <li><span>         <div>
<span class="string">"right"</span><span>><a><span class="string">"preEdit.php?id==$row[id]?>"</span><span>>编辑</span></a>  |  <a><li class="alt">
<span>             href=</span><span class="string">"delete.php?id==$row[id]?>"</span><span>>删除</span>
</li></a></span>
</div>  </span></li>
<li><span>         </span></li>
</td>  </li>
<li class="alt"><span>     </span></li>
</tr>  </span></li>
<li><span>     <tr>
<span class="string">"#ffffff"</span><span>>  </span><li class="alt">
<span>         </span><td>内容:=<span class="vars">$row</span><span>[content]?></span>
</td>  </li>
<li><span>     </span></li>
</tr>  </span></li>
<li class="alt"><span>     <tr>
<span class="string">"#ffffff"</span><span>>  </span><li>
<span>         </span><td>  <li class="alt"><span>         <div>
<span class="string">"right"</span><span>>发表日期:=</span><span class="vars">$row</span><span>[lastdate]?></span>
</div>  </span></li>
<li><span>         </span></li>
</td>  </li>
<li class="alt"><span>     </span></li>
</tr>  </span></li>
<li><span>     <?php  }  </span></span></li>
<li class="alt"><span>     }  </span></li>
<li><span>     ?>  </span></li>
<li class="alt"><span> </span></li>
</table> </span></li>
</ol>
Nach dem Login kopieren

说明:在这个小程序中,有一点不足之处在于,只能同时搜索两个关键字,并且中间用空格" "隔开,如果只是搜索一个关键字,如:"大"
显示的时候会出现乱码 ……^|_|^,这是由于下面代码的结果:

<ol class="dp-c">
<li class="alt"><span><span> </span><span class="comment">//用空格符把关键字分割开 </span><span> </span></span></li>
<li>
<span> </span><span class="vars">$key</span><span>=</span><span class="func">explode</span><span>(</span><span class="string">' '</span><span>, </span><span class="vars">$_GET</span><span>[keyWord]); </span>
</li>
</ol>
Nach dem Login kopieren

如果要改进的话,在这里的后面就要做一下判断了。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445701.htmlTechArticle项目结构: 498)this.width=498;' onmousewheel = 'javascript:return big(this)' height="183" alt="\" width="274" src="http://www.bkjia.com/uploadfile/2013/0904/2013090409540695...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage