php檢測json格式是否正確的案例

來源:互聯網
上載者:User
本文給大家分享PHP判斷json格式是否正確的實現代碼,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧

廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:


<?php$GLOBALS['count'] = 0;//校正data或者content的json格式是否有錯誤function data($value) {  if (isset($value['data'])) {    $value['data'] = json_decode($value['data'], true);  } else if (isset($value['content'])) {    $value['content'] = json_decode($value['content'], true);  } else {    die('必須要有content或者data欄位');  }  $error = json_last_error();  if (!empty($error)) {    echo "<pre>";    print_r($value);    echo "</pre>";  }  return $value;}//校正靜態資源是否存在;function my_filter($value) {  $needle = ['.jpg', '.jpeg', '.png', '.avi', '.mp4', '.wav', '.gif', '.mp3'];  $root = 'D:/phpStudy/WWW/levelData/';  foreach ($needle as $k => $v) {    $aa = strpos($value, $v);    if ($aa) {      $file = $root . $value;      if (!file_exists($file)) {        $GLOBALS['count']++;        return $value;      }    }  }}//擷取多維陣列裡面某一列的下標,並重新組成一維數組function searchMultiArray(array $array, $search, $mode = 'key') {  $res = array();  foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $key => $value) {    if ($search === ${${"mode"}}) {      if ($mode == 'key') {        $res[] = $value;      } else {        $res[] = $key;      }    }  }  return $res;}function my_filter_answer($value) {  $needle = [',', '、', ' ', '.', ',,', ',,'];  foreach ($needle as $k => $v) {    $aa = strpos($value, $v);    if ($aa) {      return 1;    }  }}//將漢字,特殊字元原樣變成json資料function ch_json_encode($data) {  $ret = ch_urlencode($data);  $ret = json_encode($ret);  return '\'' . addslashes(urldecode($ret)) . '\'';}//漢字,特殊字元變可讀懂的字串主程式function ch_urlencode($data) {  if (is_array($data) || is_object($data)) {    foreach ($data as $k => $v) {      if (is_scalar($v)) {        if (is_array($data)) {          $data[$k] = urlencode($v);        } else if (is_object($data)) {          $data->$k = urlencode($v);        }      } else if (is_array($data)) {        $data[$k] = ch_urlencode($v); // 遞迴調用該函數      } else if (is_object($data)) {        $data->$k = ch_urlencode($v);      }    }  }  return $data;}

總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.