Parameter formatted read

Source: Internet
Author: User
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
  1. Include_once (' Param.class.php ');
  2. Analog Settings Get/post data
  3. $_request[' int '] = ' 1243 ';
  4. $_request[' str '] = ' hello ';
  5. $_request[' bool '] = ' true ';
  6. $_request[' arr '] = ' 1,2,3,4 ';
  7. $_request[' json '] = Json_encode (Array (' a ' = = ' A ', ' B ' = ' B '));
  8. $_request[' Date ' = Date (' y-m-d h:i:s ');
  9. Single Data read
  10. Var_dump (param::getint (' int ', 0)); Echo '
    ';
  11. Var_dump (Param::getint (' Undefined-int ',-1)), Echo '
    ';
  12. Var_dump (param::getstr (' str ', ' Default '), Echo '
    ';
  13. Var_dump (Param::getbool (' bool ', NULL)); Echo '
    ';
  14. Var_dump (Param::getstrarray (' arr ', ', ', NULL)); Echo '
    ';
  15. Var_dump (Param::getjson (' json ', NULL)); Echo '
    ';
  16. Var_dump (Param::gettime (' Date ',-1)), Echo '
    ';
  17. Echo '
    '; Echo '
    ';
  18. Bulk acquisition
  19. $fields = Array ();
  20. $fields [] = array (' int ', Param:: $FIELD _type_int, 0);
  21. $fields [] = Array (' Undefined-int ', Param:: $FIELD _type_int,-1);
  22. $fields [] = array (' str ', Param:: $FIELD _type_str, ' Default ');
  23. $fields [] = array (' bool ', Param:: $FIELD _type_bool, 0);
  24. $fields [] = Array (' arr ', Param:: $FIELD _type_strarr, 0);
  25. $fields [] = Array (' JSON ', Param:: $FIELD _type_json, 0);
  26. $fields [] = Array (' Date ', Param:: $FIELD _type_time,-1);
  27. $data = Param::p arse ($fields);
  28. Var_dump ($data);
  29. Echo '
    '; Echo '
    ';
  30. Get Array data
  31. $source = Array (' int ' = ' 1234 ', ' str ' = ' hello ');
  32. Var_dump (param::getint (' int ',-1)); Echo '
    ';
  33. Var_dump (param::getstr (' str ', ' Default '), Echo '
    ';
  34. ?>
Copy Code
    1. Int (1243)
    2. Int (-1)
    3. String (5) "Hello"
    4. BOOL (TRUE)
    5. Array (4) {[0]=> string (1) "1" [1]=> string (1) "2" [2]=> string (1) "3" [3]=> string (1) "4"}
    6. Array (2) {["a"]=> string (1) "A" ["B"]=> string (1) "B"}
    7. Int (1408603747)
    8. 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)}
    9. Int (1243)
    10. String (5) "Hello"
Copy Code
  1. /**
  2. * Parameter Management
  3. *
  4. * @author wangaibo168@163.com
  5. * @charset Utf-8
  6. */
  7. Class Param {
  8. /**
  9. * Default constructor
  10. */
  11. Private Function __construct () {}
  12. /**
  13. * Get native data
  14. * @param $name
  15. * @param null $def
  16. * @param null $arr
  17. * @return NULL
  18. */
  19. public static function GetData ($name, $def =null, $arr =null) {
  20. if (Is_null ($name) | | $name = = = ") return $def;
  21. $name = Trim ($name);
  22. $temp = Is_array ($arr)? $arr: $_request;
  23. if (Array_key_exists ($name, $temp)) return $temp [$name];
  24. return $def;
  25. }
  26. /**
  27. * Get String data
  28. * @param $name
  29. * @param string $def
  30. * @param null $arr
  31. * @return String
  32. */
  33. public static function Getstr ($name, $def = ", $arr =null) {
  34. $value = Self::getdata ($name, $def, $arr);
  35. Return @strval ($value);
  36. }
  37. /**
  38. * Get Numeric data
  39. * @param $name
  40. * @param int $def
  41. * @param null $arr
  42. * @return int
  43. */
  44. public static function GetInt ($name, $def =0, $arr =null) {
  45. $value = Self::getdata ($name, $def, $arr);
  46. Return @intval ($value);
  47. }
  48. /**
  49. * Get Boolean type data
  50. * @param $name
  51. * @param bool $def
  52. * @param null $arr
  53. * @return BOOL
  54. */
  55. public static function Getbool ($name, $def =false, $arr =null) {
  56. $value = Self::getdata ($name, $def, $arr);
  57. if (is_string ($value)) {
  58. $value = Strtolower ($value);
  59. if ($value = = ' true ' | | $value = = ' 1 ') return true;
  60. if ($value = = ' false ' | | $value = = ' 0 ') return false;
  61. }
  62. if (Is_int ($value)) {
  63. if ($value ==1) return true;
  64. if ($value ==0) return false;
  65. }
  66. if (Is_object ($value)) {
  67. return $value!=null;
  68. }
  69. return $def;
  70. }
  71. /**
  72. * Get array type data
  73. * @param $name
  74. * @param array $def
  75. * @param null $arr
  76. * @return Array
  77. */
  78. public static function GetArray ($name, $def =array (), $arr =null) {
  79. $value = Self::getdata ($name, $def, $arr);
  80. if (!is_array ($value)) {
  81. $value = Array ($value);
  82. }
  83. return $value;
  84. }
  85. /**
  86. * Get JSON type data
  87. * @param $name
  88. * @param array $def
  89. * @param null $arr
  90. * @return Array
  91. */
  92. public static function Getjson ($name, $def =array (), $arr =null) {
  93. $value = SELF::GETSTR ($name, NULL, $arr);
  94. if ($value ==null) return $def;
  95. $value = @json_decode ($value, true);
  96. if (Is_array ($value) && count ($value) >0) return $value;
  97. return $def;
  98. }
  99. /**
  100. * Time format is 2013-12-02 11:00:11, converted to timestamp
  101. * @param $name
  102. * @param int $def
  103. * @param null $arr
  104. * @return int
  105. */
  106. public static function GetTime ($name, $def =0, $arr =null) {
  107. $value = Self::getstr ($name, ", $arr);
  108. if (empty ($value)) return $def;
  109. $value = Trim ($value);
  110. 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) ){
  111. if (Is_array ($ret) && count ($ret) ==7) {
  112. List ($t, $y, $m, $d, $h, $mi, $s) = $ret;
  113. Return Mktime ($h, $mi, $s, $m, $d, $y);
  114. }else{
  115. return $def;
  116. }
  117. }
  118. if (Preg_match ('/^ ([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) $/', $value, $ret)) {
  119. if (Is_array ($ret) && count ($ret) ==4) {
  120. List ($t, $y, $m, $d) = $ret;
  121. Return Mktime (0,0,0, $m, $d, $y);
  122. }else{
  123. return $def;
  124. }
  125. }
  126. return $def;
  127. }
  128. /**
  129. * Time format is 2013-12-02 11:00:11, converted to digital (14-bit)
  130. * @param $name
  131. * @param string $def
  132. * @param null $arr
  133. * @return bool|string
  134. */
  135. public static function GetDate ($name, $def = ' 00000000000000 ', $arr =null) {
  136. $value = Self::gettime ($name, 0, $arr);
  137. if ($value >0) {
  138. Return date (' Ymdhis ', $value);
  139. }
  140. return $def;
  141. }
  142. /**
  143. * Format string date as standard date
  144. * @param $value
  145. * @param bool $full
  146. * @param string $def
  147. * @return String
  148. */
  149. public static function FormatDate ($value, $full =false, $def = ") {
  150. if (empty ($value)) return $def;
  151. $value = Trim ($value);
  152. if (Preg_match ('/^ ([0-9]{4}) ([0-9]{2}) ([0-9]{2}) ([0-9]{2}) ([0-9]{2}] ([0-9]{2}) $/', $value, $ret)) {
  153. if (Is_array ($ret) && count ($ret) ==7) {
  154. List ($t, $y, $m, $d, $h, $mi, $s) = $ret;
  155. if ($y ==0 | | $m ==0 | | $d ==0) return $def;
  156. if (! $full && $h ==0 && $mi ==0 && $s ==0) {
  157. Return "$y-$m-$d";
  158. }
  159. Return "$y-$m-$d $h: $mi: $s";
  160. }else{
  161. return $def;
  162. }
  163. }
  164. }
  165. /**
  166. * Get floating point data
  167. * @param $name
  168. * @param int $def
  169. * @param null $arr
  170. * @return Float
  171. */
  172. public static function GetDouble ($name, $def =0, $arr =null) {
  173. $value = Self::getdata ($name, $def, $arr);
  174. Return @doubleval ($value);
  175. }
  176. /**
  177. * Gets and converts a string to an array
  178. * @param $name
  179. * @param string $limit
  180. * @param array $def
  181. * @param null $arr
  182. * @return Array
  183. */
  184. public static function Getstrarray ($name, $limit = ', ', $def =array (), $arr =null) {
  185. $value = Self::getstr ($name, ", $arr);
  186. if (empty ($value)) return $def;
  187. $arr = Explode ($limit, $value);
  188. if (!is_array ($arr)) return $def;
  189. $value = Array ();
  190. foreach ($arr as $v) {
  191. if (empty ($v)) continue;
  192. $value [] = $v;
  193. }
  194. return $value;
  195. }
  196. /**
  197. * Set native data
  198. * @param $name
  199. * @param $value
  200. */
  201. public static function SetData ($name, $value) {
  202. if (empty ($name)) return;
  203. $_get[$name] = $value;
  204. $_post[$name] = $value;
  205. $_request[$name] = $value;
  206. }
  207. /**
  208. * Set new native data based on old data
  209. * @param $name
  210. * @param $oldName
  211. */
  212. public static function Setdatabyname ($name, $oldName) {
  213. if (Empty ($name) | | empty ($OLDNAME)) return;
  214. $value = Self::getdata ($oldName);
  215. Self::setdata ($name, $value);
  216. }
  217. /**
  218. * @var string Native data type
  219. */
  220. public static $FIELD _type_data = ' DATA ';
  221. /**
  222. * @var string numeric data type
  223. */
  224. public static $FIELD _type_int = ' INT ';
  225. /**
  226. * @var string String data type
  227. */
  228. public static $FIELD _type_str = ' STR ';
  229. /**
  230. * @var string floating-point data type
  231. */
  232. public static $FIELD _type_double = ' DOUBLE ';
  233. /**
  234. * @var String Boolean data type
  235. */
  236. public static $FIELD _type_bool = ' BOOL ';
  237. /**
  238. * @var string JSON data type
  239. */
  240. public static $FIELD _type_json = ' JSON ';
  241. /**
  242. * @var string array data type
  243. */
  244. public static $FIELD _type_array = ' ARRAY ';
  245. /**
  246. * @var string timestamp data type
  247. */
  248. public static $FIELD _type_time = ' time ';
  249. /**
  250. * @var String string time data type
  251. */
  252. public static $FIELD _type_date = ' DATE ';
  253. /**
  254. * @var string string array data type
  255. */
  256. public static $FIELD _type_strarr = ' Strarr ';
  257. /**
  258. * Format data based on data fields
  259. * @param $fields
  260. * @return Array
  261. */
  262. public static function Parse ($fields) {
  263. if (!is_array ($fields) | | count ($fields) ==0) return array ();
  264. $data = Array ();
  265. foreach ($fields as $field) {
  266. if (!is_array ($field) | | | count ($field)!=3) continue;
  267. List ($name, $type, $def) = $field;
  268. if (Empty ($name) | | empty ($TYPE)) continue;
  269. $type = Strtolower ($type);
  270. if ($type ==self:: $FIELD _type_data) {
  271. $value = Self::getdata ($name, $def);
  272. }else if ($type ==self:: $FIELD _type_int) {
  273. $value = Self::getint ($name, $def);
  274. }else if ($type ==self:: $FIELD _type_str) {
  275. $value = Self::getstr ($name, $def);
  276. }else if ($type ==self:: $FIELD _type_double) {
  277. $value = self::getdouble ($name, $def);
  278. }else if ($type ==self:: $FIELD _type_bool) {
  279. $value = Self::getbool ($name, $def);
  280. }else if ($type ==self:: $FIELD _type_array) {
  281. $value = Self::getarray ($name, $def);
  282. }else if ($type ==self:: $FIELD _type_time) {
  283. $value = Self::gettime ($name, $def);
  284. }else if ($type ==self:: $FIELD _type_date) {
  285. $value = Self::getdate ($name, $def);
  286. }else if ($type ==self:: $FIELD _type_json) {
  287. $value = Self::getjson ($name, $def);
  288. }else if ($type ==self:: $FIELD _type_strarr) {
  289. $value = Self::getstrarray ($name, ', ', $def);
  290. }else{
  291. $value = $def;
  292. }
  293. $data [$name] = $value;
  294. }
  295. return $data;
  296. }
  297. /**
  298. * Format JSON data
  299. * @param $name
  300. * @param $fields
  301. * @param null $arr
  302. * @return Array
  303. */
  304. public static function Parsejson ($name, $fields, $arr =null) {
  305. if (!is_array ($fields) | | count ($fields) ==0) return array ();
  306. $data = Array ();
  307. $temp = Self::getjson ($name, NULL, $arr);
  308. if (!is_array ($temp)) $temp = Array ();
  309. foreach ($fields as $field) {
  310. if (!is_array ($field) | | | count ($field)!=3) continue;
  311. List ($name, $type, $def) = $field;
  312. if (Empty ($name) | | empty ($TYPE)) continue;
  313. $type = Strtolower ($type);
  314. if ($type ==self:: $FIELD _type_data) {
  315. $value = Self::getdata ($name, $def, $temp);
  316. }else if ($type ==self:: $FIELD _type_int) {
  317. $value = Self::getint ($name, $def, $temp);
  318. }else if ($type ==self:: $FIELD _type_str) {
  319. $value = Self::getstr ($name, $def, $temp);
  320. }else if ($type ==self:: $FIELD _type_double) {
  321. $value = self::getdouble ($name, $def, $temp);
  322. }else if ($type ==self:: $FIELD _type_bool) {
  323. $value = Self::getbool ($name, $def, $temp);
  324. }else if ($type ==self:: $FIELD _type_array) {
  325. $value = Self::getarray ($name, $def, $temp);
  326. }else if ($type ==self:: $FIELD _type_time) {
  327. $value = Self::gettime ($name, $def, $temp);
  328. }else if ($type ==self:: $FIELD _type_date) {
  329. $value = Self::getdate ($name, $def, $temp);
  330. }else if ($type ==self:: $FIELD _type_json) {
  331. $value = Self::getjson ($name, $def, $temp);
  332. }else if ($type ==self:: $FIELD _type_strarr) {
  333. $value = Self::getstrarray ($name, ', ', $def, $temp);
  334. }else{
  335. $value = $def;
  336. }
  337. $data [$name] = $value;
  338. }
  339. return $data;
  340. }
  341. }
  342. ?>
Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.