使用php怎么判斷str字符串是不是xml格式數(shù)據(jù)-創(chuàng)新互聯(lián)

使用php怎么判斷str字符串是不是xml格式數(shù)據(jù)?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)七臺(tái)河免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

具體如下:

<?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>創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,</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é)果:

$xmlstr是xml格式數(shù)據(jù)
$jsonstr不是xml格式數(shù)據(jù)

看完上述內(nèi)容,你們掌握使用php怎么判斷str字符串是不是xml格式數(shù)據(jù)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

分享題目:使用php怎么判斷str字符串是不是xml格式數(shù)據(jù)-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://bm7419.com/article36/dgoisg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、做網(wǎng)站、外貿(mào)建站、網(wǎng)站營銷、商城網(wǎng)站、網(wǎng)頁設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)