Home > Backend Development > PHP Tutorial > PHP determines whether str string is xml format data

PHP determines whether str string is xml format data

小云云
Release: 2023-03-19 19:54:01
Original
1594 people have browsed it

本文主要介绍php判断str字符串是否是xml格式数据的方法,结合实例形式较为详细的分析了php采用自定义函数针对xml格式数据进行验证的相关操作技巧,需要的朋友可以参考下,希望能帮助到大家。


<?php
//自定义xml验证函数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));
    }
}
//应用示例:
$xmlstr=<<<ETO
<?xml version="1.0" encoding="UTF-8"?>
<books>
  <book>
    <author>Jack Herrington</author>
    <title>PHP Hacks</title>
    <publisher>O&#39;Reilly</publisher>
  </book>
  <book>
    <author>Jack Herrington</author>
    <title>Podcasting Hacks</title>
    <publisher>O&#39;Reilly</publisher>
  </book>
  <book>
    <author>XML数据</author>
    <title>脚本之家</title>
    <publisher>tools.jb51.net</publisher>
  </book>
</books>
ETO;
$jsonstr=&#39;{ "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" } ] }&#39;;
if(xml_parser($xmlstr)){
 echo "\$xmlstr是xml格式数据";
}else{
 echo "\$xmlstr不是xml格式数据";
}
echo "<br/>";
if(xml_parser($jsonstr)){
 echo "\$jsonstr是xml格式数据";
}else{
 echo "\$jsonstr不是xml格式数据";
}
?>
Copy after login

运行结果:


$xmlstr是xml格式数据
$jsonstr不是xml格式数据
Copy after login

相关推荐:

PHP解析xml格式数据工具类实例分享

php如何实现数据的导入与导出xml格式的Excel的图文代码分享

Xml格式数据的生成和解析的代码详情

The above is the detailed content of PHP determines whether str string is xml format data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template