php??jason??xml

星期四, 25th 六月 2009

php??jason??xml

???????
http://tw.php.net/manual/en/tidy.isxml.php

???????????
if($doc = @DOMDocument::loadXML($post_result))

<?php

/**

 * ????XML?JSON?????
 * ????XML????JSON?????,????
 * ????????,???array
 * @author skendy  2008-12-16
 */

class checkStringFormat{
    var $arr;
    public $mark;
    /**
     * ????,???????
     *
     * @param string $str
     */

    function __construct($str){
        if($this->mark = $this->xml_parser($str)){
           $this->arr = $this->mark;
        }elseif ($this->mark = $this->json_parser($str)){
            $this->arr = $this->mark;
        }else {
            $this->arr = 'NULL';
        }
    }
 
    /**
     * ??XML??????
     *
     * @param string $str
     * @return ???????????,????false,???????XML??
     */

    function xml_parser($str){
        $xml_parser = xml_parser_create();
        if(!xml_parse($xml_parser,$str,true)){
            xml_parser_free($xml_parser);
            return false;
        }else {
            return (json_decode(json_encode(simplexml_load_string($str)),true));
        }
    } 

    /**
     * ??JSON??????
     *
     * @param string $str
     * @return ???????????,????false,???????JSON??
     */

    function json_parser($str){
        $arr = json_decode($str,true);
        if(gettype($arr) != "array"){
            return false;
        }else {
            return $arr;
        }
    }

    /**
     * ???????XML???JSON??
     *
     * @param string $str
     * @return unknown
     */

    function string_parser($str){
        $strArr = explode("&",$str);
        $len    = @count($strArr);
        if($len < 1){
            return false;
        }else{
            return false;
        }
    }

    /**
     * ???????
     */

    function display(){
        return $this->arr;
    }
}

?>