从db读取时,字符串文字中的php错误控制字符

时间:2013-10-09 02:42:44

标签: php json

我有一个带有这样一个元素的关联数组(print_r($ element)的一部分):

[przeplywy] => Array
    (
        [0] => Array
            (
                [probes] => Array
                    (
                        [0] => sonda-10

                    )

                [flow] => Array
                    (
                        [0] => Array
                            (
                                [0] => created
                                [1] => sip
                                [2] => sport
                                [3] => dip
                                [4] => dport
                                [5] => proto
                                [6] => sensor
                                [7] => os
                            )

                        [1] => Array
                            (
                                [0] => 2013-09-12 06:10:26
                                [1] => 192.168.0.1
                                [2] => 18560
                                [3] => 10.0.0.1
                                [4] => 1900
                                [5] => UDP
                                [6] => sonda-10
                                [7] => 
                            )

                        [2] => Array
                            (
                                [0] => 2013-09-12 09:31:08
                                [1] => 192.168.0.1
                                [2] => 13011
                                [3] => 10.0.0.1
                                [4] => 1900
                                [5] => UDP
                                [6] => sonda-10
                                [7] => 
                            )

                        [3] => Array
                            (
                                [0] => 2013-09-12 09:37:42
                                [1] => 192.168.0.1
                                [2] => 4813
                                [3] => 10.0.0.1
                                [4] => 1900
                                [5] => UDP
                                [6] => sonda-10
                                [7] => 
                            )

                        [4] => Array
                            (
                                [0] => 2013-09-12 11:24:41
                                [1] => 192.168.0.1
                                [2] => 50091
                                [3] => 10.0.0.1
                                [4] => 1900
                                [5] => UDP
                                [6] => sonda-10
                                [7] => 
                            )

                        [5] => Array
                            (
                                [0] => 2013-09-12 13:36:55
                                [1] => 192.168.0.1
                                [2] => 17064
                                [3] => 10.0.0.1
                                [4] => 1900
                                [5] => UDP
                                [6] => sonda-10
                                [7] => 
                            )

                    )

            )

    )

我使用json_encode($ t ['przeplywy']);将其作为longtext utf8_general_ci保存到MySQL DB中。 在DB中我可以看作:

[{"probes":["sonda-10"],"flow":[["created","sip","sport","dip","dport","proto","sensor","os"],["2013-09-12 06:10:26","192.168.0.1","18560","10.0.0.1","1900","UDP","sonda-10",""],["2013-09-12 09:31:08","192.168.0.1","13011","10.0.0.1","1900","UDP","sonda-10",""],["2013-09-12 09:37:42","192.168.0.1","4813","10.0.0.1","1900","UDP","sonda-10",""],["2013-09-12 11:24:41","192.168.0.1","50091","10.0.0.1","1900","UDP","sonda-10",""],["2013-09-12 13:36:55","192.168.0.1","17064","10.0.0.1","1900","UDP","sonda-10",""]]}]

当我尝试解码它时,我得到null并http://json.parser.online.fr/声称是因为 “字符串文字中的错误控制字符”出了什么问题?

1 个答案:

答案 0 :(得分:-2)

从查询结果中删除控制字符就行了......

preg_replace('/[\x00-\x1F\x7F]/', '',$row['przeplywy'])