Core of search engine technology (PHP version)

Source: Internet
Author: User

When talking about web search engines, most people will think of Yahoo. Indeed, Yahoo has created an Internet search age. However, Yahoo's current technology for searching web pages was not developed by the company itself. In August 2000, Yahoo adopted the technology of Google (www.google.com), a risk company created by Stanford students. The reason is very simple. Google's search engine can search for the desired information faster and more accurately than Yahoo's previous technology.

It is impossible for us to design and develop a strong and efficient search engine and database by ourselves in terms of technology and capital in a short time. However, since Yahoo is using other technologies, so can we also use other people's ready-made search engine websites?

Analysis of programming ideas

We can imagine this: simulate a query, send a search command in the corresponding format to a search engine website, return the search results, and analyze the HTML code of the results, remove unnecessary characters and code and display them in the desired format on our website page.

In this way, the key to the problem is that we need to select a search information that is accurate (so that our search will be more meaningful), fast (because we need to analyze the search results and display additional time), the search results are concise (for HTML source code analysis and stripping, thanks to the various excellent features of the new-generation search engine Google, let's take it as an example to see how PHP can be used to implement the background-based search for Google (www.google.com) and personalized display on the front-end.

Let's take a look at the composition of Google's query commands. Go to www.google.com, enter "abcd" in the query bar, click the query button, we can find that the address bar of the browser becomes: "http://www.google.com/search? Q = abcd & btnG = Google Search & hl = zh-CN & lr = ", we can see that Google uses the form get method to pass query parameters and submit query commands. We can use the file () function in PHP to simulate the query process.

Understanding File () functions

Syntax: array file (string filename );

The returned value is an array. All files are read into the array variables. The file here can be local or remote. The remote file must specify the protocol used. Example: result = file ("http://www.google.com/search? Q =... mp; hl = zh-CN & lr = "). This statement simulates the process of querying the word" abcd "on Google, and displays the search result with each behavior element, return to the result of the array variable. Because the file read here is remote, the protocol name "http: //" cannot be missing.

If you want to allow users to enter search characters for any search, you can create an input text box and submit button, and replace the searched character "abcd" with the variable above:
Echo'
'; File: // form without parameters. The default submission method is get. It is submitted to itself.
Echo ''; file: // Construct a text input box
Echo ''; file: // Construct a Submit Query button
Echo'
';

If (isset (keywords) file: // after submission, PHP will generate the variable kwywords, which requires the following program to run after submission
{
Urlencode (keywords); file: // URL encoding of user input content
Result = file ("http://www.google.com/search? Q = ". keywords." & btnG = Google Search & hl = zh-CN & lr = ");
File: // replace the query statement variables and save the query results in the array variable result.
Result_string = join ("", result); file: // combines the array $ result into a string. The elements of each array are glued with spaces.
... File: // further process
}
?>


The above program can query according to user input and synthesize the returned result into a string variable $ result_string. Please note that you must use the urlencode () function to encode the URL of the user's input content so that you can normally query the entered Chinese characters, spaces, and other special characters, this method simulates Google's query commands as much as possible to ensure the correctness of the search results.

Analysis of Google

For ease of understanding, let's assume that what we really need is the title of the search result. URL and introduction, which is a simple and typical requirement. In this way, all we need to do is to remove the head and footer of the Google search results, including a Google logo, a re-search input box, and a description of the search results, in addition, the original HTML format tags are removed from the entries in the remaining search results and replaced with the desired format.

To achieve this, we must carefully analyze the HTML source code of Google search results and find the rule. It is not hard to find that the body of the search results on Google is always included in the first source code.

Marker and penultimate

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.