This article mainly introduces PHP to determine whether STR string is an XML format data method, combined with the case of the form of a more detailed analysis of PHP using a custom function for XML format data validation of the relevant operation skills, the need for friends can refer to, hope to help everyone.
<?php//custom XML Validation function Xml_parser () 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)); }}//Application Example: $xmlstr =<<<eto<?xml version= "1.0" encoding= "UTF-8"?><books> <book> <author >jack herrington</author> <title>php hacks</title> <publisher>o ' Reilly</publisher > </book> <book> <author>jack herrington</author> <title>podcasting hacks</tit Le> <publisher>o ' reilly</publisher> </book> <book> <author>xml data </author> <title> Scripting House </title> <publisher>tools.jb51.net</publisher> </book></books>eto ; $jsonstr = ' {' Tools ': [{' Name ': ' CSS format ', ' site ': ' Http://tools.jb51.net/code/css '}, {' name ': ' JSON format ', ' site ' :"Http://tools.jb51.net/code/json"}, {"Name": "pwd Check", "Site": "Http://tools.jb51.net/password/my_password_safe" }]} '; if (Xml_parser ($XMLSTR)) {echo "\ $XMLSTR is XML format data";} else{echo "\ $XMLSTR not XML Format data";} echo "<br/>"; if (Xml_parser ($JSONSTR)) {echo "\ $JSONSTR is XML format data";} else{echo "\ $JSONSTR not XML Format data";}? >
Operation Result:
$XMLSTR is XML format data $jsonstr not XML format data