Use php to display keywords from search engines

Source: Internet
Author: User

Use php to display keywords from search engines

Display the keyword of the visitor's search on the first page of the visitor's search engine. Make some changes based on the keyword to improve the interaction ability of the website, such as displaying other articles related to this keyword.

The following is the implementation code:

The Code is as follows:

<? Php

/*

Plugin Name: display-search-keywords

Plugin URI: http://www.imyxiao.com/1531.html

Description: When a visitor comes to your blog through the search engine, this plug-in can display the keywords of the visitor's search.

Version: 1.0

Author: <a href = "http://www.imyxiao.com/"> YANG Xiao </a>

*/

Function unescape ($ str ){

$ Ret = '';

$ Len = strlen ($ str );

For ($ I = 0; $ I <$ len; $ I ++ ){

If ($ str [$ I] = '%' & $ str [$ I + 1] = 'U '){

$ Val = hexdec (substr ($ str, $ I + 2, 4 ));

If ($ val <0x7f)

$ Ret. = chr ($ val );

Else

If ($ val <0x800)

$ Ret. = chr (0xc0 | ($ val> 6). chr (0x80 | ($ val & 0x3f ));

Else

$ Ret. = chr (0xe0 | ($ val> 12 )). chr (0x80 | ($ val> 6) & 0x3f )). chr (0x80 | ($ val & 0x3f ));

 

$ I + = 5;

} Else

If ($ str [$ I] = '% '){

$ Ret. = urldecode (substr ($ str, $ I, 3 ));

$ I + = 2;

} Else

$ Ret. = $ str [$ I];

}

Return $ ret;

}

Function ls_get_delim ($ ref ){

$ Search_engines = array (

'Google .com.hk '=> 'Q ',

'Google .com.tw '=> 'Q ',

'Go .google.com '=> 'Q ',

'Google. com' => 'Q ',

'Blogsearch .google.com '=> 'Q ',

'Cn .bing.com '=> 'Q ',

'One .cn.yahoo.com '=> 'P ',

'Baidu. com' => 'wd ',

'Soso. com' => 'w ',

'Youdao. com' => 'Q ',

'Sogou. com' => 'query'

);

$ Delim = false;

// Judge

If (isset ($ search_engines [$ ref]) {

$ Delim = $ search_engines [$ ref];

}

Return $ delim;

}

 

Function ls_get_refer (){

// Determine the URL address of the previous page

$ QueryString = $ _ GET ['Referer'];

$ QueryString = unescape ($ queryString );

If (! Isset ($ queryString) | ($ queryString = ''))

Return false;

$ Referer_info = parse_url ($ queryString );

$ Referer = $ referer_info ['host'];

// Remove www.

If (substr ($ referer, 0, 4) = 'www .')

$ Referer = substr ($ referer, 4 );

Return $ referer;

}

 

Function ls_getinfo ($ what ){

$ Referer = ls_get_refer ();

If (! $ Referer)

Return false;

$ Delimiter = ls_get_delim ($ referer );

If ($ delimiter ){

$ Terms = ls_get_terms ($ delimiter );

If ($ what = 'isref '& $ terms! = ''){

Return true;

}

If ($ what = 'terms '){

Echo $ terms;

}

}

Return false;

}

 

Function yxiao_seems_utf8 ($ str ){

$ Length = strlen ($ str );

For ($ I = 0; $ I <$ length; $ I ++ ){

$ C = ord ($ str [$ I]);

If ($ c <0x80)

$ N = 0; #0 bbbbbbb

Elseif ($ c & 0xE0) = 0xC0) $ n = 1; #110 bbbbb

Elseif ($ c & 0xF0) = 0xE0) $ n = 2; #1110 bbbb

Elseif ($ c & 0xF8) = 0xF0) $ n = 3; # 11110bbb

Elseif ($ c & 0xFC) = 0xF8) $ n = 4; # 111110bb

Elseif ($ c & 0xFE) = 0xFC) $ n = 5; # 1111110b

Else

Return false; # Does not match any model

For ($ j = 0; $ j <$ n; $ j ++) {# n bytes matching 10 bbbbbb follow?

If (+ + $ I = $ length) | (ord ($ str [$ I]) & 0xC0 )! = 0x80 ))

Return false;

}

}

Return true;

}

 

Function ls_get_terms ($ d ){

// Obtain the query value

$ QueryString = $ _ GET ['Referer'];

$ QueryString = unescape ($ queryString );

$ Query_str = parse_url ($ queryString );

Parse_str ($ query_str [query], $ query_str );

$ Query = $ query_str [$ d];

$ Query = urldecode ($ query );

 

$ Query = str_replace ("'", '', $ query );

$ Query = str_replace ('"','', $ query );

$ Query_array = preg_split ('/[s, +.] +/', $ query );

$ Query_terms = implode ('', $ query_array );

$ Terms = htmlspecialchars ($ query_terms );

// Gbk-> utf8

If (! Yxiao_seems_utf8 ($ terms )){

$ Terms = iconv ("GBK", "UTF-8 // IGNORE", $ terms );

}

Return $ terms;

}

If (ls_getinfo ('isref ')){

?>

Document. write ('<div id = "serp"> <p> ')

Document. write ('more Search Results: <em> <a href = "http://www.jb51.net/search? Q = <? Php ls_getinfo (terms);?> "')

Document. write ('title = "view <? Php ls_getinfo (terms);?> Search Result "rel =" nofollow "> <? Php ls_getinfo (terms);?> </A> </em> ')

Document. write ('</p> </div> ');

<? Php }?>

 

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.