Skip to content

Instantly share code, notes, and snippets.

@wenjun1055
Last active January 9, 2019 15:30
Show Gist options
  • Save wenjun1055/8090433 to your computer and use it in GitHub Desktop.
Save wenjun1055/8090433 to your computer and use it in GitHub Desktop.

Revisions

  1. wenjun1055 renamed this gist Dec 23, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. wenjun1055 created this gist Dec 23, 2013.
    260 changes: 260 additions & 0 deletions common
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,260 @@
    <?php

    /**
    * Method dump
    * @return void
    */
    function dump() {
    echo '<pre>';
    $argument_list = func_get_args();
    foreach ($argument_list as $variable) {
    if (is_array($variable)) {
    print_r($variable);
    } else {
    var_dump($variable);
    }
    }
    echo '</pre>';
    echo "\n";
    }

    /**
    * Method underline_to_camel
    * 下划线转驼峰
    * @param $string
    * @return mixed
    */
    function underline_to_camel($string) {
    return preg_replace('/_([a-zA-Z])/e', "strtoupper('\\1')", $string);
    }

    /**
    * Method camel_to_underline
    * 驼峰转下划线
    * @param $string
    * @return string
    */
    function camel_to_underline($string) {
    return strtolower(trim(preg_replace('/([A-Z])/', "_\\1", $string), '_'));
    }

    /**
    * Method get_client_ip
    * 获取客户端IP
    * @return bool|string
    */
    function get_client_ip() {
    //验证HTTP头中是否有REMOTE_ADDR
    if (!isset($_SERVER['REMOTE_ADDR'])) {
    return '127.0.0.1';
    }

    //验证是否为非私有IP
    if (filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {
    return $_SERVER['REMOTE_ADDR'];
    }

    //验证HTTP头中是否有HTTP_CIP
    if (isset($_SERVER['HTTP_CIP'])) {
    return $_SERVER['HTTP_CIP'];
    }

    //验证HTTP头中是否有HTTP_X_FORWARDED_FOR
    if (!isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    return $_SERVER['REMOTE_ADDR'];
    }

    //定义客户端IP
    $client_ip = '';

    //获取", "的位置
    $position = strrpos($_SERVER['HTTP_X_FORWARDED_FOR'], ', ');

    //验证$position
    if (false === $position) {
    $client_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
    $client_ip = substr($_SERVER['HTTP_X_FORWARDED_FOR'], $position + 2);
    }

    //验证$client_ip是否为合法IP
    if (filter_var($client_ip, FILTER_VALIDATE_IP)) {
    return $client_ip;
    } else {
    return false;
    }
    }

    /**
    * Returns the values from a single column of the input array, identified by
    * the $columnKey.
    * Optionally, you may provide an $indexKey to index the values in the returned
    * array by the values from the $indexKey column in the input array.
    *
    * @param array $input A multi-dimensional array (record set) from which to pull
    * a column of values.
    * @param mixed $columnKey The column of values to return. This value may be the
    * integer key of the column you wish to retrieve, or it
    * may be the string key name for an associative array.
    * @param mixed $indexKey (Optional.) The column to use as the index/keys for
    * the returned array. This value may be the integer key
    * of the column, or it may be the string key name.
    *
    * @return array
    * @link http://www.php.net/manual/en/function.array-column.php since php 5.5.0
    * @link https://github.com/ramsey/array_column/blob/master/src/array_column.php
    */
    function array_column($input = null, $columnKey = null, $indexKey = null) {
    // Using func_get_args() in order to check for proper number of
    // parameters and trigger errors exactly as the built-in array_column()
    // does in PHP 5.5.
    $argc = func_num_args();
    $params = func_get_args();

    if ($argc < 2) {
    trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING);

    return null;
    }

    //add by wenyue1
    if (empty($params[0]) || empty($params[1])) {
    return array();
    }
    //added

    if (!is_array($params[0])) {
    trigger_error('array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', E_USER_WARNING);

    return null;
    }

    if (!is_int($params[1]) && !is_float($params[1]) && !is_string($params[1]) && $params[1] !== null && !(is_object($params[1]) && method_exists($params[1], '__toString'))) {
    trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);

    return false;
    }

    if (isset($params[2]) && !is_int($params[2]) && !is_float($params[2]) && !is_string($params[2]) && !(is_object($params[2]) && method_exists($params[2], '__toString'))) {
    trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);

    return false;
    }

    $paramsInput = $params[0];
    $paramsColumnKey = ($params[1] !== null) ? (string)$params[1] : null;

    $paramsIndexKey = null;
    if (isset($params[2])) {
    if (is_float($params[2]) || is_int($params[2])) {
    $paramsIndexKey = (int)$params[2];
    } else {
    $paramsIndexKey = (string)$params[2];
    }
    }

    $resultArray = array();

    foreach ($paramsInput as $row) {

    $key = $value = null;
    $keySet = $valueSet = false;

    if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
    $keySet = true;
    $key = (string)$row[$paramsIndexKey];
    }

    if ($paramsColumnKey === null) {
    $valueSet = true;
    $value = $row;
    } elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
    $valueSet = true;
    $value = $row[$paramsColumnKey];
    }

    if ($valueSet) {
    if ($keySet) {
    $resultArray[$key] = $value;
    } else {
    $resultArray[] = $value;
    }
    }

    }

    return $resultArray;
    }

    /**
    * Method array_rebuild
    * 通过新的Key重建数组索引
    *
    * @param array $array
    * @param string $key
    *
    * @return array
    */
    function array_rebuild(array $array, $key) {
    $data = array();

    if (empty($array) || empty($key)) {
    return $data;
    }

    foreach ($array as $info) {
    if (isset($info[$key])) {
    $data[$info[$key]] = $info;
    }
    }

    return $data;
    }

    /**
    * Method array_group
    * 通过新的Key分组, 并支持保留指定参数
    *
    * @param array $array
    * @param $key
    * @param array $keep_key_array
    *
    * @return array
    */
    function array_group(array $array, $key, array $keep_key_array = array()) {
    $data = array();

    if (empty($array) || empty($key)) {
    return $data;
    }

    $keep_key_array = array_flip($keep_key_array);

    foreach ($array as $info) {
    if (isset($info[$key])) {
    if (empty($keep_key_array)) {
    $data[$info[$key]][] = $info;
    } else {
    if (count($keep_key_array) > 1) {
    $data[$info[$key]][] = array_intersect_key($info, $keep_key_array);
    } else {
    $data[$info[$key]][] = $info[key($keep_key_array)];
    }
    }
    }
    }

    return $data;
    }

    /**
    * Method replace_space_char
    * 替换空格字符
    *
    * @param $string
    *
    * @return mixed
    */
    function replace_space_char($string) {
    return preg_replace('/\s+| /', '', $string);
    }