How PHP determines if the STR string is not an XML-formatted data detailed

Source: Internet
Author: User
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 custom functions for XML Format data validation of the relevant operation skills, the need for friends can refer to the next

This article describes how PHP determines whether STR strings are data in XML format. Share to everyone for your reference, as follows:

<?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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.