首頁  >  文章  >  後端開發  >  PHP-xml & jsonp轉數組的方法

PHP-xml & jsonp轉數組的方法

藏色散人
藏色散人轉載
2019-12-04 13:32:082570瀏覽

一、xml轉成數組,xml中包含706a388e39e8555a9742f4594c5343bf標籤

/**
 * 将xml转换为数组
 * @param string $xml:xml文件或字符串
 * @return array
 */
function xmlToArray($xml){
//考虑到xml文档中可能会包含<![CDATA[]]>标签,第三个参数设置为LIBXML_NOCDATA
if (file_exists($xml)) {
libxml_disable_entity_loader(false);
$xml_string = simplexml_load_file($xml,&#39;SimpleXMLElement&#39;, LIBXML_NOCDATA);
}else{
libxml_disable_entity_loader(true);
$xml_string = simplexml_load_string($xml,&#39;SimpleXMLElement&#39;, LIBXML_NOCDATA);
}
$result = json_decode(json_encode($xml_string),true);
return $result;
}

二、jsonp轉換成數組

/**
 * 把jsonp转为php数组
 * @param string $jsonp jsonp字符串
 * @param boolean $assoc 当该参数为true时,将返回array而非object
 * @return array
 */
function jsonp_decode($jsonp, $assoc = false)
{
    $jsonp = trim($jsonp);
    if(isset($jsonp[0]) && $jsonp[0] !== &#39;[&#39; && $jsonp[0] !== &#39;{&#39;) {
        $begin = strpos($jsonp, &#39;(&#39;);
        if(false !== $begin)
        {
            $end = strrpos($jsonp, &#39;)&#39;);
            if(false !== $end)
            {
                $jsonp = substr($jsonp, $begin + 1, $end - $begin - 1);
            }
        }
    }
    return json_decode($jsonp, $assoc);
}

相關推薦:《PHP教學

以上是PHP-xml & jsonp轉數組的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除