Read formatted parameters

Source: Internet
Author: User
Read formatted parameters
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

  1. Include_once ('param. class. php ');
  2. // Simulate the GET/POST data setting
  3. $ _ REQUEST ['int'] = '000000 ';
  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. // Read a single data
  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. // Obtain data in batches
  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: parse ($ fields );
  28. Var_dump ($ data );
  29. Echo'
    '; Echo'
    ';
  30. // Obtain array data
  31. $ Source = array ('int' => '123', 'str' => 'Hello ');
  32. Var_dump (Param: getInt ('int',-1); echo'
    ';
  33. Var_dump (Param: getStr ('str', 'default'); echo'
    ';
  34. ?>

  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"} ["json"] => array (2) {["a"] => string (1) "a" ["B"] => string (1) "B"} ["date"] => int (1408603747 )}
  9. Int (1243)
  10. String (5) "hello"

  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. * Obtain 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. * Getting 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. * Obtain numerical 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. * Obtain Boolean 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. * Getting array 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. * Obtain 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. * The Time format is 11:00:11 and is converted to the 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. * The Time format is 11:00:11 and is converted to a number (14 digits)
  130. * @ Param $ name
  131. * @ Param string $ def
  132. * @ Param null $ arr
  133. * @ Return bool | string
  134. */
  135. Public static function getDate ($ name, $ def = '000000', $ 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 the string date as the 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. * Getting 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. * Obtain and convert 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 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 time data type
  251. */
  252. Public static $ FIELD_TYPE_DATE = 'date ';
  253. /**
  254. * @ Var string array data type
  255. */
  256. Public static $ FIELD_TYPE_STRARR = 'strarr ';
  257. /**
  258. * Formatting 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. ?>

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.