Home > Article > Backend Development > Several commonly used PHP functions
Interception string function
/** * @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); }
This is a very easy-to-use function for intercepting strings. The input is html code. Please use the strip_tags() function to convert the code to String!
Curl encapsulation function
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); }
Everyone who has written Curl knows that you always have to write a lot before you can use it. Now the blogger has also packaged it for you. Take Go ahead and use it, even pigs should know the parameters, so no more markings!
Classification tree function can be used for classification, hierarchical relationships such as message boards, etc.
/** * 定义分类树函数 * @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; }
For more articles related to several commonly used PHP functions, please pay attention to the PHP Chinese website!