亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊 搜索引擎技術(shù)核心揭密(PHP)

搜索引擎技術(shù)核心揭密(PHP)

Jun 21, 2016 am 09:14 AM
google lt quot string

搜索引擎

作者:沙雨

編者按:這是一篇精彩的編程教學(xué)文章,不但詳細地剖析了搜索引擎的原理,也提供了筆者自己對使用PHP編制搜索引擎的一些思路。整篇文章深入淺出,相信無論是高手還是菜鳥,都能從中得到不少的啟發(fā)。

  談到網(wǎng)頁搜索引擎時,大多數(shù)人都會想到雅虎。的確,雅虎開創(chuàng)了一個互聯(lián)網(wǎng)絡(luò)的搜索時代。然而,雅虎目前用于搜索網(wǎng)頁的技術(shù)卻并非該公司原先自己開發(fā)的。2000年8月,雅虎采用了Google(www.google.com)這家由斯坦福大學(xué)學(xué)生創(chuàng)建的風(fēng)險公司的技術(shù)。理由非常簡單,Google的搜索引擎比雅虎先前使用的技術(shù)能更快、更準確搜索到所需要的信息。

  讓我們自己來設(shè)計、開發(fā)一個強勁、高效的搜索引擎和數(shù)據(jù)庫恐怕短時間內(nèi)在技術(shù)、資金等方面是不可能的,不過,既然雅虎都在使用別人的技術(shù),那么我們是不是也可以使用別人現(xiàn)成的搜索引擎網(wǎng)站呢?

剖析編程思路

  我們可以這樣設(shè)想:模擬一個查詢,向某個搜索引擎網(wǎng)站發(fā)出相應(yīng)格式的搜索命令,然后傳回搜索結(jié)果,對結(jié)果的HTML代碼進行分析,剝離多余的字符和代碼,最后按所需要的格式顯示在我們自己的網(wǎng)站頁面里。

  這樣,問題的關(guān)鍵就在于,我們要選定一個搜索信息準確(這樣我們的搜索才會更有意義?。?、速度快(因為我們分析搜索結(jié)果并顯示需要額外的時間),搜索結(jié)果簡潔(便于進行HTML源代碼分析和剝離)的搜索網(wǎng)站,由于新一代搜索引擎Google的各種優(yōu)良特性,這里我們選擇它為例,來看看用PHP怎樣實現(xiàn)后臺對Google(www.google.com)搜索、前臺個性化顯示這一過程。

  我們先來看看Google的查詢命令的構(gòu)成。進入www.google.com網(wǎng)站,在查詢欄中輸入“abcd”,點擊查詢按鈕,我們可以發(fā)現(xiàn)瀏覽器的地址欄變成:"http://www.google.com/search?q=abcd&btnG=Google%CB%D1%CB%F7&hl=zh-CN&lr=",可見,Google是通過表單的get方式來傳遞查詢參數(shù)并遞交查詢命令的。我們可以使用PHP中的file()函數(shù)來模擬這個查詢過程。

了解File()函數(shù)

  語法: array file(string filename);

  返回值為數(shù)組,將文件全部讀入數(shù)組變量中。這里的文件可以是本地的,也可以是遠程的,遠程文件必須指明所使用的協(xié)議。例如: result=file(“http://www.google.com/search?q=abcd&btnG=Google%CB%D1%CB%F7&hl=zh-CN&lr=”),該語句將模擬我們在Google上查詢單詞“abcd”的過程,并將搜索結(jié)果以每行為元素,傳回到數(shù)組變量 result中。因為這里讀取的文件是遠程的,所以協(xié)議名“http://”不能缺少。

  如果要讓用戶輸入搜索字符進行任意搜索,我們可以做一個輸入文本框和提交按鈕,并將上文中的被搜索字符“abcd”用變量替換:
echo '

'; //沒有參數(shù)的form,默認提交方式為get,提交到本身
echo ''; //構(gòu)造一個文本輸入框
echo ''; //構(gòu)造一個提交查詢按鈕
echo '
';

if (isset( keywords)) //提交后PHP會生成變量 kwywords,即要求下面的程序在提交后運行
{
urlencode( keywords); //對用戶輸入內(nèi)容進行URL編碼
result=file("http://www.google.com/search?q=". keywords."&btnG=Google%CB%D1%CB%F7&hl=zh-CN&lr=");
//對查詢語句進行變量替換,將查詢結(jié)果保存在數(shù)組變量 result中
result_string=join(" ", result); //將數(shù)組$result合并成字符串,各數(shù)組元素之間用空格粘和
... //進一步處理
}
?>

  上面的這段程序已經(jīng)能按用戶輸入內(nèi)容進行查詢,并將返回的結(jié)果合成一個字符串變量$result_string。請注意要使用urlencode()函數(shù)將用戶輸入內(nèi)容進行URL編碼,才可以正常地對輸入的漢字、空格以及其他特殊字符進行查詢,這樣做也是盡可能逼真地模擬Google的查詢命令,保證搜索結(jié)果的正確性。

對Google的分析

  為了便于理解,現(xiàn)在假設(shè)我們所真正需要的東西是:搜索結(jié)果的標題。網(wǎng)址和簡介等,這是一個簡潔而典型的需求。這樣,我們所要做的便是:去除Google搜索結(jié)果的臺頭和腳注,包括一個Google的標志、再次搜索的輸入框和搜索結(jié)果說明等,并且在剩余的搜索結(jié)果各項條目中剝離原來的HTML格式標記,替換成我們想要的格式。

  要做到這一點,我們必須仔細地分析Google搜索結(jié)果的HTML源碼,找到其中的規(guī)律。不難發(fā)現(xiàn),在Google的搜索結(jié)果的正文總是包含在源碼的第一個

標記和倒數(shù)第二個

標記之間,并且倒數(shù)第二個

標記后緊跟table字符,而且這個組合“


  以下所有程序均依次接續(xù)在上文程序的“進一步處理”處。

  result_string = strstr( result_string, "

"); //取 result_string從第一個

開始后的字符串,以去除Google臺頭
position= strpos( result_string,"

table符號的位置
result_string= substr( result_string,0, position);//截取第一個

table符號之前的字符串,以去除腳注

應(yīng)用與實現(xiàn)

  OK,現(xiàn)在我們已經(jīng)得到有用的HTML源碼主干了,剩下的問題是如何自主地顯示這些內(nèi)容。我們再分析一下這些搜索結(jié)果條目,發(fā)現(xiàn)每個條目之間也是很有規(guī)律的用
分隔,也就是各成一個段落,按這個特點我們用explode()函數(shù)把每個條目切開:

  語法:explode(string separator, string string);

  返回一個數(shù)組,按separator切開后的各個小字串被保存在數(shù)組中。

  于是:
result_array=explode("

", result_string); //用字串"

"把結(jié)果切開

  我們就得到一個數(shù)組 result_array,其中每個元素都是一個搜索結(jié)果條目。我們所要做的僅僅是研究每個條目及其HTML顯示格式代碼,然后按要求替換就行了。下面用循環(huán)來處理 result_array中的每個條目。
for( i=0; i {
... //處理每個條目
}

  對于每個條目,我們也很容易找到一些特點:每個條目都由標題、摘要、簡介、類別、網(wǎng)址等組成,每個部分都換行,即包含
標記,于是再次分割:(以下處理程序放在上文的循環(huán)中)
every_item=explode("
", result_array[ i]);

  這樣我們得到一個數(shù)組 every_item,其中 every_item[0]就是標題, every_item[1]和 every_item[2]兩行為摘要, every_item[3]和 every_item[4]等等的頭部如果包含“簡介:”、“類別:”字符,則是簡介或類別(因為有的結(jié)果條目沒有該項),如果頭部包含“”則肯定就是網(wǎng)址啦,這種對比判斷我們常使用正則表達式(略),如果要替換也很方便,比如包含標題的$every_item[0],其本身是有鏈接的,我們希望修改這個鏈接屬性,讓它在新窗口打開鏈接:
echo eregi_replace(' {
... //處理每個條目中除去第一項(第一項為標題,已經(jīng)顯示)的每一項
... //更多格式修改
}

  這樣就修改了鏈接屬性,其余很多顯示格式的修改、剝離、替換都能用正則替換eregi_replace()來完成。

  至此我們已經(jīng)得到了每個搜索條目的每一項,并能任意修改每項的格式,甚至可以給他套上漂亮的表格。然而一個好的程序應(yīng)該能適應(yīng)各種運行環(huán)境的,這里也不例外,我們其實還只是討論了搜索結(jié)果的HTML剝離的一種框架方法,真正要做得完美,還要考慮很多內(nèi)容,比如要顯示一共搜索出多少結(jié)果,分成多少頁等等,甚至還可以刨除與Google相關(guān)的那些“類別”、“簡介”等代碼,讓客戶根本看不到原始網(wǎng)站。不過這些內(nèi)容和要求我們都能通過分析HTML進行剝離得到?,F(xiàn)在大家完全能自己動手,做個極富個性化的搜索引擎啦。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Google Pixel 9 Pro XL gets tested with desktop mode Google Pixel 9 Pro XL gets tested with desktop mode Aug 29, 2024 pm 01:09 PM

Google has introduced DisplayPort Alternate Mode with the Pixel 8 series, and it's present on the newly launched Pixel 9 lineup. While it's mainly there to let you mirror the smartphone display with a connected screen, you can also use it for desktop

Google Pixel 9 and Pixel 9 Pro rumoured to gain Creative Assistant AI upon release Google Pixel 9 and Pixel 9 Pro rumoured to gain Creative Assistant AI upon release Jun 22, 2024 am 10:50 AM

Currently, four new Pixel smartphones are anticipated to land this autumn. To recap, the series is rumoured to feature thePixel 9 and Pixel 9 Pro at launch. However, the Pixel 9 Pro will be a rival to the iPhone 16 Pro rather than a Pixel 8 Pro (curr

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI has started to provide developers with access to extended context windows and cost-saving features, starting with the Gemini 1.5 Pro large language model (LLM). Previously available through a waitlist, the full 2 million token context windo

Google Tensor G4 of Pixel 9 Pro XL lags behind Tensor G2 in Genshin Impact Google Tensor G4 of Pixel 9 Pro XL lags behind Tensor G2 in Genshin Impact Aug 24, 2024 am 06:43 AM

Google recently responded to the performance concerns about the Tensor G4 of the Pixel 9 line. The company said that the SoC wasn't designed to beat benchmarks. Instead, the team focused on making it perform well in the areas where Google wants the c

Google app beta APK teardown reveals new extensions coming to Gemini AI assistant Google app beta APK teardown reveals new extensions coming to Gemini AI assistant Jul 30, 2024 pm 01:06 PM

Google's AI assistant, Gemini, is set to become even more capable, if the APK teardown of the latest update (v15.29.34.29 beta) is to be considered. The tech behemoth's new AI assistant could reportedly get several new extensions. These extensions wi

New Google Pixel desktop mode showcased in fresh video as possible Motorola Ready For and Samsung DeX alternative New Google Pixel desktop mode showcased in fresh video as possible Motorola Ready For and Samsung DeX alternative Aug 08, 2024 pm 03:05 PM

A few months have passed since Android Authority demonstrated a new Android desktop mode that Google had hidden away within Android 14 QPR3 Beta 2.1. Arriving hot on the heels of Google adding DisplayPort Alt Mode support for the Pixel 8 and Pixel 8

Google Pixel 9 smartphones will not launch with Android 15 despite seven-year update commitment Google Pixel 9 smartphones will not launch with Android 15 despite seven-year update commitment Aug 01, 2024 pm 02:56 PM

The Pixel 9 series is almost here, having been scheduled for an August 13 release. Based on recent rumours, the Pixel 9, Pixel 9 Pro and Pixel 9 Pro XL will mirror the Pixel 8 and Pixel 8 Pro (curr. $749 on Amazon) by starting with 128 GB of storage.

Google\'s new Chromecast \'TV Streamer\' rumoured to launch with Ethernet and Thread connectivity Google\'s new Chromecast \'TV Streamer\' rumoured to launch with Ethernet and Thread connectivity Aug 01, 2024 am 10:21 AM

Google is roughly a fortnight away from fully revealing new hardware. As usual, countless sources have leaked details about new Pixel devices, whether that be the Pixel Watch 3, Pixel Buds Pro 2 or Pixel 9 smartphones. It also seems that the company

See all articles