《PHP教程:php判斷str字符串是否是xml格式數(shù)據(jù)的方法示例》要點(diǎn):
本文介紹了PHP教程:php判斷str字符串是否是xml格式數(shù)據(jù)的方法示例,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
本文實(shí)例講述了php判斷str字符串是否是xml格式數(shù)據(jù)的方法.分享給大家供大家參考,具體如下:PHP應(yīng)用
<?php //自定義xml驗(yàn)證函數(shù)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)); } } //應(yīng)用示例: $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數(shù)據(jù)</author> <title>維易PHP</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是xml格式數(shù)據(jù)"; }else{ echo "\$xmlstr不是xml格式數(shù)據(jù)"; } echo "<br/>"; if(xml_parser($jsonstr)){ echo "\$jsonstr是xml格式數(shù)據(jù)"; }else{ echo "\$jsonstr不是xml格式數(shù)據(jù)"; } ?>
運(yùn)行結(jié)果:PHP應(yīng)用
$xmlstr是xml格式數(shù)據(jù) $jsonstr不是xml格式數(shù)據(jù)
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:PHP應(yīng)用
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljsonPHP應(yīng)用
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformatPHP應(yīng)用
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compressPHP應(yīng)用
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformatPHP應(yīng)用
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP針對(duì)XML文件操作技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP錯(cuò)誤與異常處理方法總結(jié)》、《PHP基本語(yǔ)法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》PHP應(yīng)用
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助.PHP應(yīng)用
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/426.html