Php implementation of the returned data formatting class instance, php instance
This article describes the php implementation of the returned data formatting class and its usage, which is very useful in string processing. Share it with you for your reference. The specific method is as follows:
The DataReturn. class. php class file is as follows:
<? Php/** return data formatting class * Date: 2011-08-15 * Author: fdipzone */class DataReturn {// class start private $ type; private $ xmlroot; private $ callback; private $ returnData; public function _ construct ($ param = array () {$ this-> type = $ this-> exists ($ param, 'type ')? Strtoupper ($ param ['type']): 'json'; // type JSON, XML, CALLBACK, ARRAY $ this-> xmlroot = $ this-> exists ($ param, 'xmlroot ')? $ Param ['xmlroot']: 'xmlroot'; // xml root dom name $ this-> callback = $ this-> exists ($ param, 'callback ')? $ Param ['callback']: ''; // JS callback function name $ format = array (); $ format ['retcode'] = $ this-> exists ($ param, 'format. retcode ')? $ Param ['format'] ['refercode']: 'refercode'; // retcode corresponding name $ format ['msg '] = $ this-> exists ($ param, 'format. msg ')? $ Param ['format'] ['msg ']: 'msg'; // msg name $ format ['data'] = $ this-> exists ($ param, 'format. data ')? $ Param ['format'] ['data']: 'data'; // the corresponding data name $ result = array (); $ result [$ format ['refercode'] = $ this-> exists ($ param, 'refercode ')? $ Param ['retcode']: 0; $ result [$ format ['msg '] = $ this-> exists ($ param, 'msg ')? $ Param ['msg ']: ''; $ result [$ format ['data'] = $ this-> exists ($ param, 'data ')? $ Param ['data']: ''; $ this-> returnData = $ result;} // output data public function data_return () {ob_clean (); switch ($ this-> type) {case 'json': $ this-> json_return (); break; case 'xml': $ this-> xml_return (); break; case 'callback': $ this-> callback_return (); break; case 'array': $ this-> array_return (); break; default: $ this-> json_return ();} exit ();} // output JSON format data. If the callback parameter exists, return the JSONP format private function json _ Return () {header ('content-type: text/html; charset = UTF-8 '); if (empty ($ this-> callback )) {echo json_encode ($ this-> returnData);} else {echo $ this-> callback. '('. json_encode ($ this-> returnData ). ');' ;}}// output data in XML format private function xml_return () {header ('content-type: text/xml; charset = UTF-8 '); echo $ this-> xml_encode ($ this-> returnData, $ this-> xmlroot);} // output JSON data and call the callback method private function callback_r Eturn () {header ('content-type: text/html; charset = UTF-8 '); $ this-> callback = empty ($ this-> callback )? 'Callback': $ this-> callback; echo "<script type = \" text/javascript \ "> \ r \ n"; echo $ this-> callback. "(". json_encode ($ this-> returnData ). "); \ r \ n"; echo "</script>" ;}// private function array_return () {header ('content-type: text/html; charset = UTF-8 '); echo' <pre> '; print_r ($ this-> returnData); echo' </pre> ';} // XML Code private function xml_encode ($ data, $ root = 'xmlroot', $ encoding = 'utf-8') {$ xml = "<? Xml version = "1.0 \" encoding = \ "". $ encoding. "\"?> \ N "; $ xml. = "<". $ root. "> \ n"; $ xml. = $ this-> data_to_xml ($ data); $ xml. = "</". $ root. ">"; return $ xml;} // convert the array to the XML format private function data_to_xml ($ data) {if (is_object ($ data )) {$ data = get_object_vars ($ data) ;}$ xml = ''; foreach ($ data as $ key =>$ val) {is_numeric ($ key) & $ key = "item id = \" $ key \ ""; $ xml. = "<$ key>"; $ xml. = (is_array ($ val) | is_object ($ val ))? $ This-> data_to_xml ($ val): $ this-> cdata ($ val); list ($ key,) = explode ('', $ key); $ xml. = "</$ key> \ n";} return $ xml;} // determines whether the data has private function exists ($ obj, $ key = '') {if ($ key = '') {return isset ($ obj )&&! Empty ($ obj);} else {$ keys = explode ('. ', $ key); for ($ I = 0, $ max = count ($ keys); $ I <$ max; $ I ++) {if (isset ($ obj [$ keys [$ I]) {$ obj = $ obj [$ keys [$ I];} else {return false ;}} return isset ($ obj )&&! Empty ($ obj) ;}/// determine whether to add <! [CDATA []> MARK private function cdata ($ val) {if (! Empty ($ val )&&! Preg_match ('/^ [A-Za-z0-9 + $]/', $ val) {$ val = '<! [CDATA ['. $ val.']> ';} return $ val ;}// class end?>
The demo sample program is as follows:
<? Php require_once ('datareturn. class. php '); $ param = array (// DataReturn parameter 'type' => 'json', // output type: JSON, XML, CALLBACK, the default value of ARRAY is JSON 'retcode' => '123', // retcode. The default value is 0 'msg '=> '', // msg, the default value is 'data' => array (// The data to be output 'id' => '000000', 'name' => 'fdipzone ', 'Gender' => 1, 'age' => 28), 'format' => array (// output data key format, default: retcode, msg, data 'retcode' => 'status', 'msg '=> 'info', 'data' => 'Result'), 'xmlroot' => 'xmlroot', // XML root node name when type = XML, the default value is xmlroot 'callback' => 'callback'/* When the callback method name type = JSON, the default value is null. If it is not null, the callback ({data}) is output }); when type = CALLBACK, the default value is callback. The page js callback method is automatically called */); $ obj = new DataReturn ($ param ); // create a DataReturn Class Object $ obj-> data_return (); // output data by format?>
I hope this article will help you learn php programming.
Format the date and time data returned by the database in php
You store the timestamp in the database, and then use the date method to format the time for processing.
Php numeric formatting output
Program code:
<? Phpecho fmt_num (2533120000 ). "<br> \ n"; echo fmt_num (123456789 ). "<br> \ n"; function fmt_num ($ n) {$ s = ($ n % 1000 ). 'Copper '; $ n = floor ($ n/24/60); $ s = ($ n % 1000 ). 'yin '. $ s; $ n = floor ($ n/24/60); $ s = ($ n % 1000 ). 'King '. $ s; $ n = floor ($ n/24/60); $ s = ($ n % 100 ). 'drill '. $ s; $ n = floor ($ n/100); return $ s ;}?>
Running example:
E: \ TEMP \ file \ exp> a. php
25 diamond 33 gold 120 silver-296 copper <br>
1 Diamond, 23 gold, 456 silver, 789 copper <br>
I have defined a function. You can modify the return format of the function as needed.