Several commonly used PHP functions

高洛峰
Release: 2023-03-04 20:30:02
Original
931 people have browsed it

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);
  }
Copy after login

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);
}
Copy after login

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;
  }
Copy after login

For more articles related to several commonly used PHP functions, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!