[PHP] 判斷json格式資料

直接利用 json_decode()

1
2
3
4
5
6
7
8
9
<?php
$str_1 = '{"number":"21","data":"abc"';
$str_2 = '<status>inherit</status><parent>113</parent>';

// output: true
echo ( json_decode($str_1) ? 'true' : 'false' );

// output: false
echo ( json_decode($str_2) ? 'true' : 'false' );
Licensed under CC BY-NC-SA 3.0 TW
comments powered by Disqus