给大家分享几个常用的PHP函数

2025-05-29 0 53

临近下班了,大约还有20分钟左右,手头没事,给大家分享几个函数。超级好用哟!

截取字符串函数

?

1

2

3

4

5

6

7

8

9

10

11
/**

* @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封装函数

?

1

2

3

4

5

6

7

8
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的都知道,总是要写一大堆才能使用,现在博主也给你封装好了,拿去用吧,参数应该猪也知道,所以不再标注!

分类树函数,可用于分类,和留言板等等之类的层级关系

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25
/**

* 定义分类树函数

* @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!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 给大家分享几个常用的PHP函数 https://www.kuaiidc.com/95340.html

相关文章

发表评论
暂无评论