Thinkphp Url中截取主域名
$url="http://hi.baidu.com/baisiye/item/b373db4faefad5ef1f19bce6";
$arr_url = parse_url($url);
$str=$arr_url['host']; //注意($str="hi.baidu.com"),且函数parse_url是php版本4以上有的函数
$condition['link'] = array('like',"%".$str."%");
$isOK=$info->where($condition)->select();//查找到重复数据返回数组,没有找到返回null
一、函数parse_url原型如下
对严重不合格的 URL,parse_url() 可能会返回 FALSE 并发出 E_WARNING。否则会返回一个
关联数组,其组成部分为(至少有一个):
scheme ->http、
host->baidu.com 、
port端口号、user、pass、path、query - 在问号 ? 之后、fragment - 在散列符号 # 之后
二、截取字符串显示
在thinkphp模板引擎里可以这样写:{$info['link']|msubstr=0,50,'utf-8',true}
首先要在Action函数中加载函数库
使用:
Load('extend');
或是在Action中自己定义,如下:
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
if(function_exists("mb_substr"))
return mb_substr($str, $start, $length, $charset);
elseif(function_exists('iconv_substr')) {
return iconv_substr($str,$start,$length,$charset);
}
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("",array_slice($match[0], $start, $length));
if($suffix){
return $slice."…";
}else{
return $slice;
} }