首页 > 语言 > PHP > 正文

php判断str字符串是否是xml格式数据的方法示例

2024-05-04 23:59:00
字体:
来源:转载
供稿:网友

本文实例讲述了php判断str字符串是否是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'Reilly</publisher>  </book>  <book>    <author>Jack Herrington</author>    <title>Podcasting Hacks</title>    <publisher>O'Reilly</publisher>  </book>  <book>    <author>XML数据</author>    <title>VeVb武林网</title>    <publisher>tools.vevb.com</publisher>  </book></books>ETO;$jsonstr='{ "tools": [ { "name":"css format" , "site":"http://tools.vevb.com/code/css" }, { "name":"json format" , "site":"http://tools.vevb.com/code/json" }, { "name":"pwd check" , "site":"http://tools.vevb.com/password/my_password_safe" } ] }';if(xml_parser($xmlstr)){ echo "/$xmlstr是xml格式数据";}else{ echo "/$xmlstr不是xml格式数据";}echo "<br/>";if(xml_parser($jsonstr)){ echo "/$jsonstr是xml格式数据";}else{ echo "/$jsonstr不是xml格式数据";}?>

运行结果:

$xmlstr是xml格式数据$jsonstr不是xml格式数据

希望本文所述对大家PHP程序设计有所帮助。


注:相关教程知识阅读请移步到PHP教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选