php中文网最新课程
每日17点准时技术干货分享
一、xml转成数组,xml中包含标签
/** * 将xml转换为数组 * @param string $xml:xml文件或字符串 * @return array */function xmlToArray($xml){//考虑到xml文档中可能会包含标签,第三个参数设置为LIBXML_NOCDATAif (file_exists($xml)) {libxml_disable_entity_loader(false);$xml_string = simplexml_load_file($xml,'SimpleXMLElement', LIBXML_NOCDATA);}else{libxml_disable_entity_loader(true);$xml_string = simplexml_load_string($xml,'SimpleXMLElement', 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] !== '[' && $jsonp[0] !== '{') { $begin = strpos($jsonp, '('); if(false !== $begin) { $end = strrpos($jsonp, ')'); if(false !== $end) { $jsonp = substr($jsonp, $begin + 1, $end - $begin - 1); } } } return json_decode($jsonp, $assoc);}
-END-
▼