Home  >  Article  >  Backend Development  >  对淘宝URL中ID提取的PHP代码_php实例

对淘宝URL中ID提取的PHP代码_php实例

WBOY
WBOYOriginal
2016-06-07 17:23:46881browse

复制代码 代码如下:

$taobao = 'taobao.com';
$tmall = 'tmall.com';
$guojitmall = 'tmall.hk';
$juhuasuan = 'ju.taobao.com';

/*
取宝贝的id

休闲e族

*/
function quid($strurl) {
    $strurl = strtolower ( $strurl );
    if (strpos ( $strurl, 'id' ) !== false) {
        $arr = explode ( '?', $strurl );
        $arr = explode ( '&', $arr [1] );
        $NO = 0;
        foreach ( $arr as $k => $v ) {
            if (is_string ( $v )) {
                //判断是否含有id
                if (strpos ( $v, 'id' ) !== false) {
                    //处理含有item或者num项 返还id数
                    if (strpos ( $v, 'item' ) !== false || strpos ( $v, 'num' ) !== false) {
                        //echo $v,'
';
                        $i = strrpos ( $v, '=' );
                        $str = substr ( $v, $i + 1 );
                        if (is_numeric ( $str )) {
                            return $NO = $str;
                        }
                    } else {
                        //echo $v,'
';
                        $i = strrpos ( $v, '=' );
                        $str = substr ( $v, $i + 1 );
                        $x = strlen ( $str );
                        if (is_numeric ( $str )) {
                            if ($x ==11) {
                                $NO = $str;
                            } else if ($NO == 0 || ($x > 9 && $x                                 $NO = $str;
                            }
                        }
                    }
                }
            }
        }
        return $NO;
    }
}
?>

Statement:
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