Home Backend Development PHP Tutorial Parameter format reading

Parameter format reading

Jul 25, 2016 am 08:47 AM

As a phper, I just found out today that from joining OSC to now, the only code I have shared is php. I felt red and shared a common class I wrote in the project.

GET data/POST data /Ordinary array returns formatted data through key
http://www.du52.com/text.php?id=581
  1. include_once('Param.class.php');
  2. // Simulate setting 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 reading
  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 '
    ';
  18. // Get batches of
  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. // 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" } ["json"]=> 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 raw 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 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. * 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. * The 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. * The time format is 2013-12-02 11:00:11, 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='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. * Get and convert string to 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 numerical 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 according to 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. ?>
复制代码


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles