Output formatting processing

From , 2 Years ago, written in PHP, viewed 217 times.
URL https://pastebin.vip/view/15c00b52
  1. <?php
  2.  
  3. final class out {
  4.  
  5.         /**
  6.         * 数组输出的格式
  7.         */
  8.         public function out_data($data,$type=null,$encode=null){
  9.                 $type = strtolower($type);
  10.                
  11.                 if($encode && $encode!='UTF-8')
  12.                 $data = $this->iconvs($data,'GBK','UTF-8');
  13.                
  14.                 $type = strtolower($type);
  15.                
  16.                 if(!$type){
  17.                         if(!empty($_GET['out']))$type=$_GET['out'];
  18.                         else
  19.                         $type='json';
  20.                 }
  21.                 if($type=='json'){
  22.                         if(!headers_sent())
  23.                         header('Content-Type:application/json');
  24.                         echo json_encode($data);
  25.                 }elseif($type=='print'){
  26.                         if(!headers_sent())
  27.                         header("Content-type:text/html;Charset=utf-8");
  28.                         echo '<pre>';print_r($data);echo '</pre>';
  29.                 }elseif($type=='serialize'){
  30.                         if(!headers_sent())
  31.                         header("Content-type:text/html;Charset=utf-8");
  32.                         echo serialize($data);
  33.                 }elseif($type=='var_export'){
  34.                         if(!headers_sent())
  35.                         header("Content-type:text/html;Charset=utf-8");
  36.                         var_export($data);
  37.                 }elseif($type=='var_dump'){
  38.                         if(!headers_sent())
  39.                         header("Content-type:text/html;Charset=utf-8");
  40.                         var_dump($data);
  41.                 }elseif($type=='xml'){
  42.                         if(!headers_sent())
  43.                         header("Content-type:text/xml;Charset=utf-8");
  44.                         echo self::arrtoxml($data);            
  45.                 }else{
  46.                         if(!headers_sent())
  47.                         header("Content-type:text/html;Charset=utf-8");
  48.                         echo self::outtable($data);
  49.                 }
  50.                 exit();
  51.         }
  52.  
  53.         public function arrtoxml($arr,$dom=0,$item=0){
  54.                 if (!$dom){
  55.                 $dom = new DOMDocument("1.0");
  56.                 }
  57.                 if(!$item){
  58.                 $item = $dom->createElement("root");
  59.                 $dom->appendChild($item);
  60.                 }
  61.                 foreach ($arr as $key=>$val){
  62.                        
  63.                         $itemx = $dom->createElement(is_string($key)?(is_numeric(substr($key,0,1))?'_'.$key:$key):"item");
  64.                         $item->appendChild($itemx);
  65.                         if (!is_array($val) && !is_object($val)){
  66.                                 $text = $dom->createTextNode($val);
  67.                                 $itemx->appendChild($text);
  68.                         }else {
  69.                                 if(is_object($val)){
  70.                                        
  71.                                 }
  72.                                 self::arrtoxml($val,$dom,$itemx);
  73.                         }
  74.                 }
  75.                 return $dom->saveXML();
  76.         }
  77.         /**
  78.          * xml转换成数组
  79.          * @param type $contents
  80.          * @param type $get_attributes
  81.          * @param type $priority
  82.          * @return type
  83.          */
  84.         public function xml2array($contents, $get_attributes=1, $priority = 'tag') {
  85.                 if (!$contents)
  86.                         return array();
  87.        
  88.                 if (!function_exists('xml_parser_create')) {
  89.                         //print "'xml_parser_create()' function not found!";
  90.                         return array();
  91.                 }
  92.        
  93.                 //Get the XML parser of PHP - PHP must have this module for the parser to work
  94.                 $parser = xml_parser_create('');
  95.                 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
  96.                 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  97.                 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  98.                 xml_parse_into_struct($parser, trim($contents), $xml_values);
  99.                 xml_parser_free($parser);
  100.        
  101.                 if (!$xml_values)
  102.                         return; //Hmm
  103.        
  104.        
  105.        
  106.        
  107.        
  108.                        
  109.         //Initializations
  110.                 $xml_array = array();
  111.                 $parents = array();
  112.                 $opened_tags = array();
  113.                 $arr = array();
  114.        
  115.                 $current = &$xml_array; //Refference
  116.                 //Go through the tags.
  117.                 $repeated_tag_index = array(); //Multiple tags with same name will be turned into an array
  118.                 foreach ($xml_values as $data) {
  119.                         unset($attributes, $value); //Remove existing values, or there will be trouble
  120.                         //This command will extract these variables into the foreach scope
  121.                         // tag(string), type(string), level(int), attributes(array).
  122.                         extract($data); //We could use the array by itself, but this cooler.
  123.        
  124.                         $result = array();
  125.                         $attributes_data = array();
  126.        
  127.                         if (isset($value)) {
  128.                                 if ($priority == 'tag')
  129.                                         $result = $value;
  130.                                 else
  131.                                         $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
  132.                         }
  133.        
  134.                         //Set the attributes too.
  135.                         if (isset($attributes) and $get_attributes) {
  136.                                 foreach ($attributes as $attr => $val) {
  137.                                         if ($priority == 'tag')
  138.                                                 $attributes_data[$attr] = $val;
  139.                                         else
  140.                                                 $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  141.                                 }
  142.                         }
  143.        
  144.                         //See tag status and do the needed.
  145.                         if ($type == "open") {//The starting of the tag '<tag>'
  146.                                 $parent[$level - 1] = &$current;
  147.                                 if (!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
  148.                                         $current[$tag] = $result;
  149.                                         if ($attributes_data)
  150.                                                 $current[$tag . '_attr'] = $attributes_data;
  151.                                         $repeated_tag_index[$tag . '_' . $level] = 1;
  152.        
  153.                                         $current = &$current[$tag];
  154.                                 } else { //There was another element with the same tag name
  155.                                         if (isset($current[$tag][0])) {//If there is a 0th element it is already an array
  156.                                                 $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  157.                                                 $repeated_tag_index[$tag . '_' . $level]++;
  158.                                         } else {//This section will make the value an array if multiple tags with the same name appear together
  159.                                                 $current[$tag] = array($current[$tag], $result); //This will combine the existing item and the new item together to make an array
  160.                                                 $repeated_tag_index[$tag . '_' . $level] = 2;
  161.        
  162.                                                 if (isset($current[$tag . '_attr'])) { //The attribute of the last(0th) tag must be moved as well
  163.                                                         $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  164.                                                         unset($current[$tag . '_attr']);
  165.                                                 }
  166.                                         }
  167.                                         $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
  168.                                         $current = &$current[$tag][$last_item_index];
  169.                                 }
  170.                         } elseif ($type == "complete") { //Tags that ends in 1 line '<tag />'
  171.                                 //See if the key is already taken.
  172.                                 if (!isset($current[$tag])) { //New Key
  173.                                         $current[$tag] = $result;
  174.                                         $repeated_tag_index[$tag . '_' . $level] = 1;
  175.                                         if ($priority == 'tag' and $attributes_data)
  176.                                                 $current[$tag . '_attr'] = $attributes_data;
  177.                                 } else { //If taken, put all things inside a list(array)
  178.                                         if (isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array
  179.                                                 // push the new element into that array.
  180.                                                 $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  181.        
  182.                                                 if ($priority == 'tag' and $get_attributes and $attributes_data) {
  183.                                                         $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  184.                                                 }
  185.                                                 $repeated_tag_index[$tag . '_' . $level]++;
  186.                                         } else { //If it is not an array
  187.                                                 $current[$tag] = array($current[$tag], $result); //Make it an array using using the existing value and the new value
  188.                                                 $repeated_tag_index[$tag . '_' . $level] = 1;
  189.                                                 if ($priority == 'tag' and $get_attributes) {
  190.                                                         if (isset($current[$tag . '_attr'])) { //The attribute of the last(0th) tag must be moved as well
  191.                                                                 $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  192.                                                                 unset($current[$tag . '_attr']);
  193.                                                         }
  194.        
  195.                                                         if ($attributes_data) {
  196.                                                                 $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  197.                                                         }
  198.                                                 }
  199.                                                 $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
  200.                                         }
  201.                                 }
  202.                         } elseif ($type == 'close') { //End of tag '</tag>'
  203.                                 $current = &$parent[$level - 1];
  204.                         }
  205.                 }
  206.        
  207.                 return($xml_array);
  208.         }      
  209.        
  210.         /**
  211.          * 将數組输出为表格
  212.          * @param array $games 数组内容
  213.          * @param array $keys  数组标题,不指定则默认以数组KEY作为标题
  214.          * @param array $showTitle 是否显示标题
  215.          */
  216.         public function outtable($games, $keys=null,$showTitle=true) {
  217.                 if($keys==null){
  218.                         if(!is_array($games) || count($games)<1){
  219.                                
  220.                                 return print_r($games,true);
  221.                         }
  222.                         if(!$games[key($games)]){
  223.                                 $echo .= 'Error Format';
  224.                                 return;
  225.                         }
  226.                         if(is_array(current($games))){
  227.                                 $keys = array_keys(current($games));
  228.                         }else{
  229.                                   $echo .= '<table  cellspacing="1" cellpadding="3" border="0" class="table0">';
  230.                                    $i = 0;
  231.                                         foreach ($games as $game_id => $game) {
  232.                                                 $i++;
  233.                                                 $echo .= '<tr bgcolor="' . (($i % 2) ? "#EEEEEE" : '#FFFFFF') . '">';
  234.                                                    $echo .= '<th align="right">'.$game_id.'</th>';
  235.                                                         $echo .= '<td>';
  236.                                                         if (is_array($game))
  237.                                                                 $echo .= self::outtable($game);
  238.                                                         else
  239.                                                                 $echo .= $game;
  240.                                                         $echo .= '</td>';
  241.                                        
  242.                                                 $echo .= '</tr>';
  243.                                         }
  244.                                         $echo .= '</table>';
  245.                                         return;
  246.                         }
  247.                 }
  248.                 if(!is_array($games)){
  249.                                 return print_r($games,true);
  250.                 }
  251.                        
  252.                 $echo .= '<table  cellspacing="1" cellpadding="3" border="0" class="table0">';
  253.                 $echo .= '<tr id=head style="">';
  254.            if($showTitle){
  255.                         $echo .= '<th>key</th>';
  256.                         foreach ($keys as $k => $v) {
  257.                                 $echo .= '<th>';
  258.                                 $echo .= $v;
  259.                                 $echo .= '</th>';
  260.                         }
  261.                         $echo .= '</tr>';
  262.            }    $i = 0;
  263.                 foreach ($games as $game_id => $game) {
  264.                         $i++;
  265.        
  266.                         $echo .= '<tr bgcolor="' . (($i % 2) ? "#EEEEEE" : '#FFFFFF') . '">';
  267.                         if($showTitle)   $echo .= '<th>'.$game_id.'</th>';
  268.                        
  269.                         if(is_array($game)){
  270.                         foreach ($keys as $k => $v) {
  271.                                 $echo .= '<td>';
  272.                                 if (is_array($game[$v]))
  273.                                         $echo .= self::outtable($game[$v],null,false);
  274.                                 else
  275.                                         $echo .= $game[$v];
  276.                                 $echo .= '</td>';
  277.                         }
  278.                         }else{
  279.                                 $echo .= '<td colspan="'.count($keys).'">';
  280.                                         $echo .= $game;
  281.                                 $echo .= '</td>';
  282.                         }
  283.                        
  284.                         $echo .= '</tr>';
  285.                 }
  286.                 $echo .= '</table>';
  287.                 return $echo ;
  288.         }
  289.  
  290.         //一个切割函数
  291.         public function cut($file, $from, $end) //字符串切割函数.
  292.         {
  293.                 if(!$file)return $file;
  294.           $message = explode($from, $file);
  295.           if(count($message)>1)
  296.           $message = explode($end, $message[1]);
  297.           else return '';
  298.           return $message[0];
  299.        
  300.         }
  301.        
  302.         public function formatHTML($html){
  303.                         $html = preg_replace('/[\n\r\t]/', ' ', $html);
  304.                         $html = preg_replace('/\s(?=\s)/', '', $html);
  305.                         $html = str_replace('> ','>',$html);
  306.                         $html = str_replace(' <','<',$html);
  307.                         $html = str_replace(' > ','>',$html);
  308.                         return $html;  
  309.         }
  310.  
  311.                 //使用配置去采集内容          
  312.        
  313.         public function iconvs($var,$encode,$outcode){
  314.                 if(is_object($var) || is_array($var)){
  315.                         foreach($var as $k=>$v){
  316.                                         unset($var[$k]);
  317.                                         $var[iconv($encode,$outcode,$k)]=$this->iconvs($v,$encode,$outcode);
  318.                         }
  319.                 }else{
  320.                         $var =iconv($encode,$outcode,$var);
  321.                 }
  322.                 return $var;
  323.         }
  324.        
  325. }
  326.  
  327. ?>

Reply to "Output formatting processing"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website