As a phper, I discovered today that from joining osc to the present, there is only one php article to share various code. I am blushing and share a common class I wrote in the project.
GET Data/POST data/normal array return formatted data through key Http://www.du52.com/text.php? Id = 581
- Include_once ('param. class. php ');
- // Simulate the GET/POST data setting
- $ _ REQUEST ['int'] = '000000 ';
- $ _ 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 ');
- // Read a single data
- 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' ';
- // Obtain data in batches
- $ 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: parse ($ fields );
- Var_dump ($ data );
- Echo'
'; Echo' ';
- // Obtain array data
- $ Source = array ('int' => '123', 'str' => 'Hello ');
- Var_dump (Param: getInt ('int',-1); echo'
';
- Var_dump (Param: getStr ('str', 'default'); echo'
';
- ?>
- 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"} ["json"] => array (2) {["a"] => string (1) "a" ["B"] => string (1) "B"} ["date"] => int (1408603747 )}
- Int (1243)
- String (5) "hello"
- /**
- * Parameter management
- *
- * @ Author wangaibo168@163.com
- * @ Charset UTF-8
- */
- Class Param {
- /**
- * Default constructor
- */
- Private function _ construct (){}
- /**
- * Obtain 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;
- }
- /**
- * Getting 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 );
- }
- /**
- * Obtain numerical 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 );
- }
- /**
- * Obtain Boolean 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;
- }
- /**
- * Getting array 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;
- }
- /**
- * Obtain 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;
- }
- /**
- * The Time format is 11:00:11 and is converted to the 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;
- }
- /**
- * The Time format is 11:00:11 and is converted to a number (14 digits)
- * @ Param $ name
- * @ Param string $ def
- * @ Param null $ arr
- * @ Return bool | string
- */
- Public static function getDate ($ name, $ def = '000000', $ arr = null ){
- $ Value = self: getTime ($ name, 0, $ arr );
- If ($ value> 0 ){
- Return date ('ymdhis ', $ value );
- }
- Return $ def;
- }
- /**
- * Format the string date as the 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;
- }
- }
- }
- /**
- * Getting 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 );
- }
- /**
- * Obtain and convert 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 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 time data type
- */
- Public static $ FIELD_TYPE_DATE = 'date ';
- /**
- * @ Var string array data type
- */
- Public static $ FIELD_TYPE_STRARR = 'strarr ';
- /**
- * Formatting 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;
- }
- }
- ?>
|