php 检测数据类型

1. 检测字符串是否为xml

    /**
     * 判断是否为xml格式
     */
    function isXml($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));
        }
    }

2. 检测字符串是否为json格式

function isJson($string) {
 json_decode($string);
 return (json_last_error() == JSON_ERROR_NONE);
}

参照:https://stackoverflow.com/questions/6041741/fastest-way-to-check-if-a-string-is-json-in-php

猜你喜欢

转载自blog.csdn.net/eaglejiawo1120/article/details/81124589
今日推荐