通用 PHP 轮子


<?php

/**

 * 判断是否 ajax 请求

 * [@return](https://learnku.com/users/31554) bool

 */

function isAjax()

{
   return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ('xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']));
}

/**

 * 获取请求参数

 * @param $param

 * @param string $method

 * @param string $default

 * [@return](https://learnku.com/users/31554) string

 * @date 2017-07-27

 */

function input($param, $method = 'all', $default = '')

{

​    $method = strtolower($method);

​    $method = in_array($method, array('get', 'post', 'all')) ? $method : 'all';

​    switch ($method) {

​        case 'get' :

​            $params = $_GET;

​            break;

​        case 'post' :

​            $params = $_POST;

​            break;

​        default :

​            $params = array_merge($_GET, $_POST);

​            break;

​    }

​    $ret = isset($params[$param]) ? $params[$param] : $default;

​    return stripslashes($ret);

}

// 获取不为空的 p 标签的值,并且在 200 字以内 
private function get_first_p($content)
{

  preg_match_all("/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/",$content,$matchs);
  foreach($matchs[0] as $match){
    $match = preg_replace("#<[^>]+>#","$2",$match);
    $match = trim($match);
  if(!empty($match))
   {
     return mb_substr($match,0,200);
   }
}
return "";
}
php
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 2

代码的缩进呢?看着好难受

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!