Home > Backend Development > PHP Tutorial > PHP function to obtain search engine inbound keywords

PHP function to obtain search engine inbound keywords

WBOY
Release: 2016-07-25 08:57:27
Original
1034 people have browsed it
This article introduces a piece of code that can obtain search engine inbound keywords. Friends in need can refer to it.

The code is as follows:

<?php
//*
* 获取来自搜索引擎入站关键词
* edit by bbs.it-home.org
*/
function get_keyword($url,$kw_start)//函数作用:从url中提取关键词。参数说明:url及关键词前的字符。
{
 $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;
}
$url=isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'';//获取入站url。
$search_1="google.com"; //q=
$search_2="baidu.com"; //wd=

$google=preg_match("/\b{$search_1}\b/",$url);//记录匹配情况,用于入站判断。
$baidu=preg_match("/\b{$search_2}\b/",$url);
$s_s_keyword="";
 if ($google)
 {//来自google
  $s_s_keyword=get_keyword($url,'q=');//关键词前的字符为“q=”。
  $s_s_keyword=urldecode($s_s_keyword);
  //$s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//引擎为gbk
 }
 else if($baidu)
 {//来自百度
  $s_s_keyword=get_keyword($url,'wd=');//关键词前的字符为“wd=”。
  $s_s_keyword=urldecode($s_s_keyword);
  $s_s_keyword=iconv("GBK","UTF-8",$s_s_keyword);//引擎为gbk
 }
 
echo '$s_s_keyword';
?>
Copy after login

Code description: 1. Pay attention to the problem of character encoding, because different search engines may return different encoding formats. 2. For a more comprehensive function, please refer to this article: Obtain search engine keyword sources.



source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template