这篇文章介绍了对淘宝URL中ID提取的PHP代码,有需要的朋友可以参考一下.

<?php
$taobao = ‘taobao.com’;
$tmall = ‘tmall.com’;
$guojitmall = ‘tmall.hk’;
$juhuasuan = ‘ju.taobao.com’;
/*
获取淘宝的宝贝Id
—By 水木(4u4v.cn)
*/
function taobaoid($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,'<br/>’;
$i = strrpos ( $v, ‘=’ );
$str = substr ( $v, $i + 1 );
if (is_numeric ( $str )) {
return $NO = $str;
}
} else {
//echo $v,'<br/>’;
$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 < 11)) {
$NO = $str;
}
}
}
}
}
}
return $NO;
}
}
//这里可以做一个输入框供用户输入宝贝链接
$strurl = “http://item.taobao.com/item.htm?spm=a1z10.1.w4-4798729943.1.VRJp5i&id=18787672995”;
echo “该链接所指向的宝贝ID是:”.taobaoid($strurl);
?>