As a phper, only today, from joining the OSC to the present, sharing a variety of code, there is only one PHP. circumnavigated blush, share a common class written by yourself in the project.
Get data/post data/normal array return formatted data via key http://www.du52.com/text.php?id=581
- Include_once (' Param.class.php ');
- Analog Settings Get/post data
- $_request[' int '] = ' 1243 ';
- $_request[' str '] = ' hello ';
- $_request[' bool '] = ' true ';
- $_request[' arr '] = ' 1,2,3,4 ';
- $_request[' json '] = Json_encode (Array (' a ' = = ' A ', ' B ' = ' B '));
- $_request[' Date ' = Date (' y-m-d h:i:s ');
- Single Data read
- Var_dump (param::getint (' int ', 0)); Echo '
';
- Var_dump (Param::getint (' Undefined-int ',-1)), Echo '
';
- Var_dump (param::getstr (' str ', ' Default '), Echo '
';
- Var_dump (Param::getbool (' bool ', NULL)); Echo '
';
- Var_dump (Param::getstrarray (' arr ', ', ', NULL)); Echo '
';
- Var_dump (Param::getjson (' json ', NULL)); Echo '
';
- Var_dump (Param::gettime (' Date ',-1)), Echo '
';
- Echo '
'; Echo ' ';
- Bulk acquisition
- $fields = Array ();
- $fields [] = array (' int ', Param:: $FIELD _type_int, 0);
- $fields [] = Array (' Undefined-int ', Param:: $FIELD _type_int,-1);
- $fields [] = array (' str ', Param:: $FIELD _type_str, ' Default ');
- $fields [] = array (' bool ', Param:: $FIELD _type_bool, 0);
- $fields [] = Array (' arr ', Param:: $FIELD _type_strarr, 0);
- $fields [] = Array (' JSON ', Param:: $FIELD _type_json, 0);
- $fields [] = Array (' Date ', Param:: $FIELD _type_time,-1);
- $data = Param::p arse ($fields);
- Var_dump ($data);
- Echo '
'; Echo ' ';
- Get Array data
- $source = Array (' int ' = ' 1234 ', ' str ' = ' hello ');
- Var_dump (param::getint (' int ',-1)); Echo '
';
- Var_dump (param::getstr (' str ', ' Default '), Echo '
';
- ?>
Copy Code
- Int (1243)
- Int (-1)
- String (5) "Hello"
- BOOL (TRUE)
- Array (4) {[0]=> string (1) "1" [1]=> string (1) "2" [2]=> string (1) "3" [3]=> string (1) "4"}
- Array (2) {["a"]=> string (1) "A" ["B"]=> string (1) "B"}
- Int (1408603747)
- Array (7) {["int"]=> int (1243) ["Undefined-int"]=> Int ( -1) ["str"]=> string (5) "Hello" ["bool"]=> bool (TRUE) ["Arr"]=> Array (4) {[0]=> string (1) "1" [1]=> string (1) "2" [2]=> string (1) "3" [3]=> string (1) "4"} ["JS On "]=> Array (2) {[" a "]=> string (1)" A "[" B "]=> string (1)" B "} [" Date "]=> int (1408603747)}
- Int (1243)
- String (5) "Hello"
Copy Code
- /**
- * Parameter Management
- *
- * @author wangaibo168@163.com
- * @charset Utf-8
- */
- Class Param {
- /**
- * Default constructor
- */
- Private Function __construct () {}
- /**
- * Get native data
- * @param $name
- * @param null $def
- * @param null $arr
- * @return NULL
- */
- public static function GetData ($name, $def =null, $arr =null) {
- if (Is_null ($name) | | $name = = = ") return $def;
- $name = Trim ($name);
- $temp = Is_array ($arr)? $arr: $_request;
- if (Array_key_exists ($name, $temp)) return $temp [$name];
- return $def;
- }
- /**
- * Get String data
- * @param $name
- * @param string $def
- * @param null $arr
- * @return String
- */
- public static function Getstr ($name, $def = ", $arr =null) {
- $value = Self::getdata ($name, $def, $arr);
- Return @strval ($value);
- }
- /**
- * Get Numeric data
- * @param $name
- * @param int $def
- * @param null $arr
- * @return int
- */
- public static function GetInt ($name, $def =0, $arr =null) {
- $value = Self::getdata ($name, $def, $arr);
- Return @intval ($value);
- }
- /**
- * Get Boolean type data
- * @param $name
- * @param bool $def
- * @param null $arr
- * @return BOOL
- */
- public static function Getbool ($name, $def =false, $arr =null) {
- $value = Self::getdata ($name, $def, $arr);
- if (is_string ($value)) {
- $value = Strtolower ($value);
- if ($value = = ' true ' | | $value = = ' 1 ') return true;
- if ($value = = ' false ' | | $value = = ' 0 ') return false;
- }
- if (Is_int ($value)) {
- if ($value ==1) return true;
- if ($value ==0) return false;
- }
- if (Is_object ($value)) {
- return $value!=null;
- }
- return $def;
- }
- /**
- * Get array type data
- * @param $name
- * @param array $def
- * @param null $arr
- * @return Array
- */
- public static function GetArray ($name, $def =array (), $arr =null) {
- $value = Self::getdata ($name, $def, $arr);
- if (!is_array ($value)) {
- $value = Array ($value);
- }
- return $value;
- }
- /**
- * Get JSON type data
- * @param $name
- * @param array $def
- * @param null $arr
- * @return Array
- */
- public static function Getjson ($name, $def =array (), $arr =null) {
- $value = SELF::GETSTR ($name, NULL, $arr);
- if ($value ==null) return $def;
- $value = @json_decode ($value, true);
- if (Is_array ($value) && count ($value) >0) return $value;
- return $def;
- }
- /**
- * Time format is 2013-12-02 11:00:11, converted to timestamp
- * @param $name
- * @param int $def
- * @param null $arr
- * @return int
- */
- public static function GetTime ($name, $def =0, $arr =null) {
- $value = Self::getstr ($name, ", $arr);
- if (empty ($value)) return $def;
- $value = Trim ($value);
- if (Preg_match ('/^ ([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) \s+ ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}) $/', $value, $ret) ){
- if (Is_array ($ret) && count ($ret) ==7) {
- List ($t, $y, $m, $d, $h, $mi, $s) = $ret;
- Return Mktime ($h, $mi, $s, $m, $d, $y);
- }else{
- return $def;
- }
- }
- if (Preg_match ('/^ ([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) $/', $value, $ret)) {
- if (Is_array ($ret) && count ($ret) ==4) {
- List ($t, $y, $m, $d) = $ret;
- Return Mktime (0,0,0, $m, $d, $y);
- }else{
- return $def;
- }
- }
- return $def;
- }
- /**
- * Time format is 2013-12-02 11:00:11, converted to digital (14-bit)
- * @param $name
- * @param string $def
- * @param null $arr
- * @return bool|string
- */
- public static function GetDate ($name, $def = ' 00000000000000 ', $arr =null) {
- $value = Self::gettime ($name, 0, $arr);
- if ($value >0) {
- Return date (' Ymdhis ', $value);
- }
- return $def;
- }
- /**
- * Format string date as standard date
- * @param $value
- * @param bool $full
- * @param string $def
- * @return String
- */
- public static function FormatDate ($value, $full =false, $def = ") {
- if (empty ($value)) return $def;
- $value = Trim ($value);
- if (Preg_match ('/^ ([0-9]{4}) ([0-9]{2}) ([0-9]{2}) ([0-9]{2}) ([0-9]{2}] ([0-9]{2}) $/', $value, $ret)) {
- if (Is_array ($ret) && count ($ret) ==7) {
- List ($t, $y, $m, $d, $h, $mi, $s) = $ret;
- if ($y ==0 | | $m ==0 | | $d ==0) return $def;
- if (! $full && $h ==0 && $mi ==0 && $s ==0) {
- Return "$y-$m-$d";
- }
- Return "$y-$m-$d $h: $mi: $s";
- }else{
- return $def;
- }
- }
- }
- /**
- * Get floating point data
- * @param $name
- * @param int $def
- * @param null $arr
- * @return Float
- */
- public static function GetDouble ($name, $def =0, $arr =null) {
- $value = Self::getdata ($name, $def, $arr);
- Return @doubleval ($value);
- }
- /**
- * Gets and converts a string to an array
- * @param $name
- * @param string $limit
- * @param array $def
- * @param null $arr
- * @return Array
- */
- public static function Getstrarray ($name, $limit = ', ', $def =array (), $arr =null) {
- $value = Self::getstr ($name, ", $arr);
- if (empty ($value)) return $def;
- $arr = Explode ($limit, $value);
- if (!is_array ($arr)) return $def;
- $value = Array ();
- foreach ($arr as $v) {
- if (empty ($v)) continue;
- $value [] = $v;
- }
- return $value;
- }
- /**
- * Set native data
- * @param $name
- * @param $value
- */
- public static function SetData ($name, $value) {
- if (empty ($name)) return;
- $_get[$name] = $value;
- $_post[$name] = $value;
- $_request[$name] = $value;
- }
- /**
- * Set new native data based on old data
- * @param $name
- * @param $oldName
- */
- public static function Setdatabyname ($name, $oldName) {
- if (Empty ($name) | | empty ($OLDNAME)) return;
- $value = Self::getdata ($oldName);
- Self::setdata ($name, $value);
- }
- /**
- * @var string Native data type
- */
- public static $FIELD _type_data = ' DATA ';
- /**
- * @var string numeric data type
- */
- public static $FIELD _type_int = ' INT ';
- /**
- * @var string String data type
- */
- public static $FIELD _type_str = ' STR ';
- /**
- * @var string floating-point data type
- */
- public static $FIELD _type_double = ' DOUBLE ';
- /**
- * @var String Boolean data type
- */
- public static $FIELD _type_bool = ' BOOL ';
- /**
- * @var string JSON data type
- */
- public static $FIELD _type_json = ' JSON ';
- /**
- * @var string array data type
- */
- public static $FIELD _type_array = ' ARRAY ';
- /**
- * @var string timestamp data type
- */
- public static $FIELD _type_time = ' time ';
- /**
- * @var String string time data type
- */
- public static $FIELD _type_date = ' DATE ';
- /**
- * @var string string array data type
- */
- public static $FIELD _type_strarr = ' Strarr ';
- /**
- * Format data based on data fields
- * @param $fields
- * @return Array
- */
- public static function Parse ($fields) {
- if (!is_array ($fields) | | count ($fields) ==0) return array ();
- $data = Array ();
- foreach ($fields as $field) {
- if (!is_array ($field) | | | count ($field)!=3) continue;
- List ($name, $type, $def) = $field;
- if (Empty ($name) | | empty ($TYPE)) continue;
- $type = Strtolower ($type);
- if ($type ==self:: $FIELD _type_data) {
- $value = Self::getdata ($name, $def);
- }else if ($type ==self:: $FIELD _type_int) {
- $value = Self::getint ($name, $def);
- }else if ($type ==self:: $FIELD _type_str) {
- $value = Self::getstr ($name, $def);
- }else if ($type ==self:: $FIELD _type_double) {
- $value = self::getdouble ($name, $def);
- }else if ($type ==self:: $FIELD _type_bool) {
- $value = Self::getbool ($name, $def);
- }else if ($type ==self:: $FIELD _type_array) {
- $value = Self::getarray ($name, $def);
- }else if ($type ==self:: $FIELD _type_time) {
- $value = Self::gettime ($name, $def);
- }else if ($type ==self:: $FIELD _type_date) {
- $value = Self::getdate ($name, $def);
- }else if ($type ==self:: $FIELD _type_json) {
- $value = Self::getjson ($name, $def);
- }else if ($type ==self:: $FIELD _type_strarr) {
- $value = Self::getstrarray ($name, ', ', $def);
- }else{
- $value = $def;
- }
- $data [$name] = $value;
- }
- return $data;
- }
- /**
- * Format JSON data
- * @param $name
- * @param $fields
- * @param null $arr
- * @return Array
- */
- public static function Parsejson ($name, $fields, $arr =null) {
- if (!is_array ($fields) | | count ($fields) ==0) return array ();
- $data = Array ();
- $temp = Self::getjson ($name, NULL, $arr);
- if (!is_array ($temp)) $temp = Array ();
- foreach ($fields as $field) {
- if (!is_array ($field) | | | count ($field)!=3) continue;
- List ($name, $type, $def) = $field;
- if (Empty ($name) | | empty ($TYPE)) continue;
- $type = Strtolower ($type);
- if ($type ==self:: $FIELD _type_data) {
- $value = Self::getdata ($name, $def, $temp);
- }else if ($type ==self:: $FIELD _type_int) {
- $value = Self::getint ($name, $def, $temp);
- }else if ($type ==self:: $FIELD _type_str) {
- $value = Self::getstr ($name, $def, $temp);
- }else if ($type ==self:: $FIELD _type_double) {
- $value = self::getdouble ($name, $def, $temp);
- }else if ($type ==self:: $FIELD _type_bool) {
- $value = Self::getbool ($name, $def, $temp);
- }else if ($type ==self:: $FIELD _type_array) {
- $value = Self::getarray ($name, $def, $temp);
- }else if ($type ==self:: $FIELD _type_time) {
- $value = Self::gettime ($name, $def, $temp);
- }else if ($type ==self:: $FIELD _type_date) {
- $value = Self::getdate ($name, $def, $temp);
- }else if ($type ==self:: $FIELD _type_json) {
- $value = Self::getjson ($name, $def, $temp);
- }else if ($type ==self:: $FIELD _type_strarr) {
- $value = Self::getstrarray ($name, ', ', $def, $temp);
- }else{
- $value = $def;
- }
- $data [$name] = $value;
- }
- return $data;
- }
- }
- ?>
Copy Code |