Php code for retrieving keywords and source search engine names

Source: Internet
Author: User
Sometimes we need to know which search engine the user uses to access our page through a keyword. of course, js can also be implemented, but here we will introduce the php implementation code. The code is as follows:






Greengnn codes




// Obtain the keyword and source search engine name

$ Search_url = isset ($ _ GET ['URL'])? $ _ GET ['URL']: ''; // indicates the input address.
/* $ Search_url = urldecode ($ search_url );
Print_r ($ search_url );
*/

$ Config = array (
"S1" => array (
"Domain" => "google.com ",
"Kw" => "q ",
"Charset" => "UTF-8"
),
"S3" => array (
"Domain" => "google.cn ",
"Kw" => "q ",
"Charset" => "UTF-8"
),
"S4" => array (
"Domain" => "baidu.com ",
"Kw" => "wd ",
"Charset" => "gbk"
),
"S5" => array (
"Domain" => "soso.com ",
"Kw" => "q ",
"Charset" => "UTF-8"
),
"S6" => array (
"Domain" => "yahoo.com ",
"Kw" => "q ",
"Charset" => "UTF-8"
),
"S7" => array (
"Domain" => "bing.com ",
"Kw" => "q ",
"Charset" => "UTF-8"
),
"S8" => array (
"Domain" => "sogou.com ",
"Kw" => "query ",
"Charset" => "gbk"
),
"S9" => array (
"Domain" => "youdao.com ",
"Kw" => "q ",
"Charset" => "UTF-8"
),
);

// Function: extract keywords from URLs. Parameter description: the url and the characters before the keyword.
Function get_keyword ($ url, $ kw_start)

{
$ Start = stripos ($ url, $ kw_start );
$ Url = substr ($ url, $ start + strlen ($ kw_start ));
$ Start = stripos ($ url ,'&');
If ($ start> 0)
{
$ Start = stripos ($ url ,'&');
$ S_s_keyword = substr ($ url, 0, $ start );
}
Else
{
$ S_s_keyword = substr ($ url, 0 );
}
Return $ s_s_keyword;
}

$ Arr_key = array ();
Foreach ($ config as $ item ){
$ Sh = preg_match ("/\ B {$ item ['domain ']} \ B/", $ search_url );
If ($ sh ){
$ Query = $ item ['kw ']. "= ";

$ S_s_keyword = get_keyword ($ search_url, $ query );
$ F_Skey = urldecode ($ s_s_keyword );
If ($ item ['charset'] = "UTF-8 "){
$ F_Skey = iconv ("UTF-8", "gb2312 // IGNORE", $ F_Skey); // final extracted keywords
}
$ Keys = explode ("", $ F_Skey );
$ Arr_key [$ item ['domain '] = $ keys;
}
}
Echo"

"; 
print_r($arr_key);
?>

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.