临近下班了,大约还有20分钟左右,手头没事,给大家分享几个函数。超级好用哟!
截取字符串函数
/** * @param string $begin 开始字符串 * @param string $end结束字符串 * @param string $str需要截取的字符串 * @return string */function get_str($begin,$end,$str){$b = mb_strpos($str,$begin) + mb_strlen($begin);$e = mb_strpos($str,$end) - $b;return mb_substr($str,$b,$e);}
这是一个非常好用的截取字符串的函数,入过是html代码,请先用strip_tags()函数将代码转为字符串!
Curl封装函数function curlGet($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); return curl_exec($ch); }
写过Curl的都知道,总是要写一大堆才能使用,现在博主也给你封装好了,拿去用吧,参数应该猪也知道,所以不再标注!
分类树函数,可用于分类,和留言板等等之类的层级关系
/** * 定义分类树函数 * @param items 需要分类的二维数组* @param $id 主键(唯一ID) * @param $belong_id 关联主键的PID * @son 可以自定义往里面插入就行 */function catagory($items,$id="id",$belong_id="belong_id",$son = "children"){$tree = array(); //格式化的树$tmpMap = array(); //临时扁平数据 foreach ($items as $item) {$tmpMap[$item[$id]] = $item;} foreach ($items as $item) {if (isset($tmpMap[$item[$belong_id]])) {$tmpMap[$item[$belong_id]][$son][] = &$tmpMap[$item[$id]];} else {$tree[] = &$tmpMap[$item[$id]];}}unset($tmpMap);return $tree;}
好的~博主下班踢球去了~
bye,see you!