PHP 判断字符串是不是xml格式
今天在今日头条上看到一篇文章:在这里分享一下,留着以后用
$xml_parser = xml_parser_create(); //建立一个 XML 解析器
f(!xml_parse($xml_parser,$result,true)){ //开始解析一个 XML 文档 成功时返回1,失败时返回0
xml_parser_free($xml_parser); //释放指定的 XML 解析器
$std = json_decode($result);
}else {
//simplexml_load_string 将XML字符串解释为对象
$std = json_decode(json_encode(simplexml_load_string($result,’SimpleXMLElement’, LIBXML_NOCDATA | LIBXML_NOBLANKS)));
}
$result 是一个字符串。大概意思是:
先通过 xml_parser_create() 函数创建 XML 解析器
然后再通过 xml_parse() 函数解析 XML 文档。
如果解析成功 就通过 simplexml_load_string 函数结合 json_encode,json_decode 转成对象,
否则直接通过 json_decode 转成对象。
标签:PHP, XML