PHP 版本 > 5.2,json_encode 自帶轉義。若是是舊版本的 PHP 則能夠用下面的函數 # list from www.json.org: (\b backspace, \f formfeed) public function escapeJsonString($value) { $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c"); $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b"); $result = str_replace($escapers, $replacements, $value); return $result; }