简体   繁体   English

如何从PHP中的双引号和斜杠清除json字符串

[英]how to clean a json string from double quotes and slashes in php

I have problem with json string in php. 我在php中遇到json字符串问题。 I have an app the get user contacts and send it to server and I process contacts in my way. 我有一个获取用户联系人并将其发送到服务器的应用程序,我用自己的方式处理联系人。

the problem is that some contacts name are very wired and some error occurred while using json_decode() contacts like this 问题是某些联系人名称连接非常json_decode()并且在使用json_decode()联系人时发生了一些错误

[
   {"displayName":"Altin"'''''"''''"""""\n\""\p chapar","phoneNumbers":[{"value":"0411*******"}]},
   {"displayName":"A""""""basi","phoneNumbers":[{"value":"0914******"}]}
]

how can I clean this dirty json string to work properly? 我如何清理这个肮脏的json字符串才能正常工作? I've tried to remove """ and \\\\// but I saw a huge amount of errors 我试图删除"""\\\\//但是看到了很多错误

thanks 谢谢

try this 尝试这个

    $result = str_split($contacts);

    for ($i = 0; $i < count($result); $i++)
    {
        $found = false;

        if ($result[$i] == '"')
        {
            if ($result[$i + 1] != ':' && $result[$i + 1] != ',' 
                && $result[$i - 1] != '{' && $result[$i - 1] != '"' 
                    && $result[$i + 1] != '}' && $result[$i - 1] != ':' 
                        && $result[$i - 1] != ',')
                $result[$i] = '';
        }
    }

    $edited = implode("", $result);
    $edited = str_replace("'" , '' , $edited);
    $edited = str_replace("\\" , '' , $edited);
    $edited = str_replace("\n" , '' , $edited);
    print_r(json_decode($edited));

this will replace all inner double quotes so the string will be clean for the json_decode to produce std_objects. 这将替换所有内部双引号,因此该字符串对于json_decode产生std_objects而言将是干净的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM