每天学习一点点,收录一点点,收集一些常用(通用)的功能函数,有助项目的快速开发,以下收录的功能函数也是我在项目中实践过的没有问题的。列一下收录过的目录
- 递归函数(还有一种写法 见第3页)
- HTML 代码 压缩
- 从文章内容(HTML 代码)中取出所有图片
递归函数
/** * 递归 * @param $array 处理的数据 * @param string $fieldName 本级的字段 * @param string $parentFieldName 父级的字段 * @param int $parentId 父级的值 * @return array */ function recursion($array, $fieldName='', $parentFieldName ='', $parentId = 0){ $arr = array(); $temp = array(); foreach ($array as $v) { if ($v[$parentFieldName] == $parentId) { $temp = recursion($array, $fieldName, $parentFieldName, $v[$fieldName]); //判断是否存在子数组 if($temp) { $v['child'] = $temp; } $arr[] = $v; } } return $arr; }
HTML 代码压缩
/** * HTML 代码进行压缩 * @param text}string $data 要处理的内容信息 * @return string */ function htmlCodeZip($higrid_uncompress_html_source) { $chunks = preg_split('/(<pre.*?\/pre>)/ms', $higrid_uncompress_html_source, -1, PREG_SPLIT_DELIM_CAPTURE); $higrid_uncompress_html_source = ''; //[]修改压缩html : 清除换行符,清除制表符,去掉注释标记 foreach ($chunks as $c) { if (strpos($c, '<pre') !== 0) { //[] remove new lines & tabs $c = preg_replace('/[\\n\\r\\t]+/', ' ', $c); // [] remove extra whitespace $c = preg_replace('/\\s{2,}/', ' ', $c); // [] remove inter-tag whitespace $c = preg_replace('/>\\s</', '><', $c); // [] remove CSS & JS comments $c = preg_replace('/\\/\\*.*?\\*\\//i', '', $c); } $higrid_uncompress_html_source .= $c; } return $higrid_uncompress_html_source; }
从文章内容中获取图片
/** * 从文章内容中获取图片 * @param $content 文章内容(HTML 代码) * @return bool|mixed */ function htmlGetImgsinarticle($content) { $temp = array(); preg_match_all("/(src|SRC)=[\"|'| ]{0,}((.*).(gif|jpg|jpeg|bmp|png))/isU", $content, $temp); if (isArray($temp[0]) == FALSE) { return FALSE; } foreach ($temp[0] as $key => $value) { $temp[0][$key] = substr($value, 5); } return $temp[0]; }
转载请注明:隨習筆記 » PHP 常用功能函数(收集中)