mysql url到domain的转化函数

使用举例:
select func_url2domain('http://iamge.baidu.com/i?ct=503316480&z=0&tn=baiduimagedetail');
代码如下,也可下载附件啊

CREATE FUNCTION `func_url2domain`(url varchar(250)) RETURNS varchar(250) CHARSET utf8
READS SQL DATA
begin

DECLARE tmpdata varchar(250);
DECLARE tmpdata2 varchar(250);

set tmpdata=url;

-- delete http://
if Locate('http://',url)=1 then
set tmpdata=Substring(url,8);
end if;

-- delete https://
if Locate('https://',tmpdata)=1 then
set tmpdata=Substring(tmpdata,9);
end if;

-- delete www.
if Locate('www.',tmpdata)=1 then
set tmpdata=Substring(tmpdata,5);
end if;

-- delete str after /
if Locate('/',tmpdata)>0 then
set tmpdata=Substring(tmpdata,1,Locate('/',tmpdata)- 1);
end if;

-- delete second domain /
set tmpdata2=Substring(tmpdata,Locate('.',tmpdata)+1);

if Locate('com',tmpdata2)=1 or Locate('cn',tmpdata2)=1 or Locate('net',tmpdata2)=1 or Locate('org',tmpdata2)=1 or Locate('biz',tmpdata2)=1 or Locate('info',tmpdata2)=1 or Locate('cc',tmpdata2)=1 or Locate('tv',tmpdata2)=1 or Locate('in',tmpdata2)=1 or Locate('us',tmpdata2)=1 or Locate('hk',tmpdata2)=1 then
set tmpdata2=tmpdata;
else
set tmpdata=tmpdata2;
set tmpdata2=Substring(tmpdata,Locate('.',tmpdata)+1);
if Locate('com',tmpdata2)=1 or Locate('cn',tmpdata2)=1 or Locate('net',tmpdata2)=1 or Locate('org',tmpdata2)=1 or Locate('biz',tmpdata2)=1 or Locate('info',tmpdata2)=1 or Locate('cc',tmpdata2)=1 or Locate('tv',tmpdata2)=1 or Locate('in',tmpdata2)=1 or Locate('us',tmpdata2)=1 or Locate('hk',tmpdata2)=1 then
set tmpdata2=tmpdata;
else
set tmpdata=tmpdata2;
end if;
end if;

return lower(tmpdata);

end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值