Home Backend Development PHP Tutorial 直接可以拿来用的PHP惯用功能代码片段(11~15)

直接可以拿来用的PHP惯用功能代码片段(11~15)

Jun 13, 2016 pm 12:34 PM
browser elseif info

直接可以拿来用的PHP常用功能代码片段(11~15)

文章来源:jquery教程?-?http://www.jq-school.com/Show.aspx?id=325

?

前面已经分享了PHP常用功能代码片段(1~5)PHP常用功能代码片段(6~10),今天是第三篇,也就是第11到15这5个实现代码片段,希望可以帮到jquery学堂群里面的成员和广大对PHP开发的网友们提高开发效率,以下是第三篇文章。

11、PHP实现如何获取网址的PR值

/*
*功能:对URL进行编码
*参数说明:$web_url 网站URL,不包含"http://"
*/
function HashURL($url)
{   
    $SEED = "Mining PageRank is AGAINST GOOGLE’S TERMS OF SERVICE. Yes, I’m talking to you, scammer.";
    $Result = 0x01020345;
    for ($i=0; $i<strlen($url); $i++) 
    {
        $Result ^= ord($SEED{$i%87}) ^ ord($url{$i});
        $Result = (($Result >> 23) & 0x1FF) | $Result << 9;
    }
    return sprintf("8%x", $Result);
}
/*
*功能:根据google提供的pr查询接口获取pagerank
*参数说明:$domain 网站域名,不包含"http://"
*/
function pagerank($domain)
{    
    $StartURL = "http://toolbarqueries.google.com/tbr?client=navclient-auto&features=Rank:&q=info:";
    $GoogleURL = $StartURL.$domain. '&ch='.HashURL($domain);
    echo $GoogleURL.'<br>';
    $fcontents = file_get_contents("$GoogleURL");
    $pagerank = substr($fcontents,9);
    if (!$pagerank) return "0";else return $pagerank;
}
//调用方法
echo pagerank("www.jq-school.com");
Copy after login


12、PHP实现为post过来的数据转换编码

$info = array();
foreach($_POST as $key => $value){
	$info[$key] = iconv("utf-8","gb2312",$value);
}
Copy after login


13、PHP实现获取客户端IP地址及所在地区

function address($ip) {
    $info = json_decode(file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip='.$ip.'&format=json'), false);
    if ($info ->ret == 1) {
        if ($info ->province != $info ->city) {
            return $info ->country.",".$info ->province."(".$info ->city.")  ".$info ->district."  ".$info ->desc;
        } else {
            return $info ->country.",".$info ->province."  ".$info ->district."  ".$info ->desc;
        }
    } else {
        return '地球';
    }
}
Copy after login


14、PHP实现取得客户端浏览器版本

function browser($ua) {
    if (stripos($ua, "Googlebot")) {
        $browser = "谷歌蜘蛛";
    }
    elseif(stripos($ua, "Baiduspider") !== false) {
        $browser = "百度蜘蛛";
    }
    elseif(stripos($ua, "Yahoo!") !== false) {
        $browser = "雅虎蜘蛛";
    }
    elseif(stripos($ua, "bingbot")) {
        $browser = "必应蜘蛛";
    }
    elseif(stripos($ua, "YRSpider")) {
        $browser = "云壤蜘蛛";
    }
    elseif(stripos($ua, "Yeti") !== false) {
        $browser = "Naver蜘蛛";
    }
    elseif(stripos($ua, "Maxthon")) {
        if (stripos($ua, "AppleWebKit")) {
            $browser = "遨游浏览器(极速模式)";
        }
        elseif(stripos($ua, "Trident")) {
            $browser = "遨游浏览器(兼容模式)";
        }
        elseif(stripos($ua, "MAXTHON 2.0")) {
            $browser = "遨游浏览器2.0";
        }
    }
    elseif(stripos($ua, "Firefox")) {
        $browser = "火狐浏览器";
    }
    elseif(stripos($ua, "Opera") == 0 && stripos($ua, "Presto")) {
        $browser = "Opera";
    }
    elseif(stripos($ua, "BIDUBrowser")) {
        if (stripos($ua, "Trident")) {
            $browser = "百度浏览器(兼容模式)";
        }
        elseif(stripos($ua, "AppleWebKit")) {
            $browser = "百度浏览器(极速模式)";
        }
    }
    elseif(stripos($ua, "Ruibin")) {
        $browser = "瑞影浏览器";
    }
    elseif(stripos($ua, "qihu theworld")) {
        if (stripos($ua, "Trident")) {
            $browser = "世界之窗浏览器";
        }
        elseif(stripos($ua, "AppleWebKit")) {
            $browser = "世界之窗浏览器(极速模式)";
        }
    }
    elseif(stripos($ua, "MetaSr")) {
        if (stripos($ua, "Trident")) {
            $browser = "搜狗高速浏览器(兼容模式)";
        }
        elseif(stripos($ua, "AppleWebKit")) {
            $browser = "搜狗高速浏览器(极速模式)";
        }
    }
    elseif(stripos($ua, "LBBROWSER")) {
        if (stripos($ua, "Trident")) {
            $browser = "猎豹浏览器(兼容模式)";
        }
        elseif(stripos($ua, "AppleWebKit")) {
            $browser = "猎豹浏览器(极速模式)";
        }
    }
    elseif(stripos($ua, "YLMFBR")) {
        $browser = "115浏览器";
    }
    elseif(stripos($ua, "QQBrowser")) {
        if (stripos($ua, "Trident")) {
            $browser = "QQ浏览器(兼容模式)";
        }
        elseif(stripos($ua, "AppleWebKit")) {
            $browser = "QQ浏览器(极速模式)";
        }
    }
    elseif(stripos($ua, "TencentTraveler")) {
        $browser = "腾讯TT浏览器";
    }
    elseif(stripos($ua, "TaoBrowser")) {
        if (stripos($ua, "Trident")) {
            $browser = "淘宝浏览器(兼容模式)";
        }
        elseif(stripos($ua, "AppleWebkit")) {
            $browser = "淘宝浏览器(极速模式)";
        }
    }
    elseif(stripos($ua, "CoolNovo")) {
        $browser = "枫树浏览器";
    }
    elseif(stripos($ua, "SaaYaa")) {
        $browser = "闪游浏览器";
    }
    elseif(stripos($ua, "360SE")) {
        $browser = "360安全浏览器";
    }
    elseif(stripos($ua, "360EE")) {
        if (stripos($ua, "Trident")) {
            $browser = "360极速浏览器(兼容模式)";
        }
        elseif(stripos($ua, "AppleWebkit")) {
            $browser = "360极速浏览器(极速模式)";
        }
    }
    elseif(stripos($ua, "Konqueror")) {
        $browser = "Konqueror";
    }
    elseif(stripos($ua, "Chrome")) {
        $browser = "谷歌浏览器";
    }
    elseif(stripos($ua, "Safari")) {
        $browser = "Safari";
    }
    elseif(stripos($ua, "MSIE")) {
        $ver = explode(";", substr($ua, stripos($ua, "MSIE") + 5, 4));
        $ver = $ver[0];
        $browser = "IE ".$ver;
    }
    elseif(stripos($ua, "UCWEB")) {
        $browser = "UCWEB浏览器";
    }
    elseif(stripos($ua, "WAP")) {
        $browser = "Mobile浏览器";
    } else {
        $browser = $ua;
    }
    if ($browser == '') $browser = $ua;
    return $browser;
}
Copy after login


15、PHP实现取得客户端操作系统版本

function os($ua) {
    $os = "";
    if (stripos($ua, "Googlebot")) {
        $os = "谷歌蜘蛛";
    }
    elseif(stripos($ua, "Baiduspider") !== false) {
        $os = "百度蜘蛛";
    }
    elseif(stripos($ua, "Yahoo!") !== false) {
        $os = "雅虎蜘蛛";
    }
    elseif(stripos($ua, "bingbot")) {
        $os = "必应蜘蛛";
    }
    elseif(stripos($ua, "YRSpider")) {
        $os = "云壤蜘蛛";
    }
    elseif(stripos($ua, "Yeti") !== false) {
        $os = "Naver蜘蛛";
    }
    elseif(stripos($ua, "Windows NT")) {
        switch (substr($ua, stripos($ua, "Windows NT") + 11, 3)) {
        case 5.0:
            {
                $os = "Windows 2000";
                break;
            }
        case 5.1:
            {
                $os = "Windows XP";
                break;
            }
        case 5.2:
            {
                $os = "Windows 2003";
                break;
            }
        case 6.0:
            {
                $os = "Windows Vista/2008";
                break;
            }
        case 6.1:
            {
                $os = "Windows 7";
                break;
            }
        case 6.2:
            {
                $os = "Windows 8";
                break;
            }
        default:
            {
                $os = "Windows";
                break;
            }
        }
        if (stripos($ua, "WOW64")) {
            $os.= "(X64)";
        } else {
            $os.= "(X86)";
        }
    }
    elseif(stripos($ua, "Android")) {
        $os = substr($ua, stripos($ua, "Android"), 11);
    }
    elseif(stripos($ua, "Linux")) {
        if (stripos($ua, "i686")) {
            $os = "Linux X86";
        } else {
            $os = "Linux";
        }
        if (stripos($ua, "X11")) {
            $os.= "(X Window)";
        }
    }
    elseif(stripos($ua, "Macintosh")) {
        $os = "Mac";
    }
    elseif(stripos($ua, "IOS")) {
        $os = "iOS";
    }
    elseif(stripos($ua, "ZTE")) {
        $os = "ZTE";
    }
    elseif(stripos($ua, "Windows 98")) {
        $os = "Windows 98";
    } else {
        $os = "未知系统";
    }
    return $os;
}
Copy after login

?

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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

See all articles