Example of how php checks whether the str string is xml-format data, strxml

Source: Internet
Author: User

Example of how php checks whether the str string is xml-format data, strxml

This example describes how php judges whether the str string is in xml format. We will share this with you for your reference. The details are as follows:

<? Php // custom xml verification 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 </title> <publisher> O 'Reilly </publisher> </book> <author> XML data </author> <title> helper house </title> <publisher> tools.jb51.net </publisher> </book> </books> ETO; $ jsonstr = '{"tools": [{"name": "css format", "site": "http://tools.j B51.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 data not in xml format";} echo "<br/>"; if (xml_parser ($ jsonstr )) {echo "\ $ jsonstr is xml format data";} else {echo "\ $ jsonstr is not xml format data" ;}?>

Running result:

$ Xmlstr is data in xml format $ jsonstr is not data in xml format

PS: Here are some online tools for xml operations for your reference:

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

Online formatting XML/online compression XML:
Http://tools.jb51.net/code/xmlformat

XMLOnline compression/formatting tools:
Http://tools.jb51.net/code/xml_format_compress

XMLCode Online formatting and beautification tools:
Http://tools.jb51.net/code/xmlcodeformat

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.