回覆列表
  • 1 # 云云科技社

    最新php判斷資料是不是JSON格式示例 以下是三零網為大家整理的最新php判斷資料是不是JSON格式示例的文章,希望大家能夠喜歡! 首先要記住json_encode返回的是字串, 而json_decode返回的是物件 判斷資料不是JSON格式: function is_not_json($str){ return is_null(json_decode($str)); } 判斷資料是合法的json資料: (PHP版本大於5.3) function is_json($string) { json_decode($string); return (json_last_error() == JSON_ERROR_NONE); } json_last_error()函式返回資料編解碼過程中發生的錯誤 注意: json編解碼所操作字串必須是UTF8的 例子 /** * 解析json串 * @param type $json_str * @return type */ function analyJson($json_str) { $json_str = str_replace("\\", "", $json_str); $out_arr = array();preg_match("/{.*}/", $json_str, $out_arr); if (!empty($out_arr)) { $result = json_decode($out_arr[0], TRUE); } else { return FALSE; } return $result; } 如果不是json則返回false

  • 中秋節和大豐收的關聯?
  • 詹姆斯若被交易,值多少籌碼?