duanlu5055 2015-12-03 07:29
浏览 381

如何删除除逗号(,),冒号(:),双引号(“)和字符串PHP的花括号之外的所有特殊字符和空格

How can i clean this JSON String data ?. At first look it is very easy using str_replace method but its not. This JSON string is form JSON object that has spaces for ex . {" First Name ": "something"} . so when i converted this into json string the empty spaces replaced by unwanted strings(\u00a0) . I think this problem can be solve using preg_replace but the suddenly i dont know the regex for comma,double quote,colon. This characters is necessary for json string format. Please help me.

for example

       {"AS_applicant_Data__c":
            "{
                \"Last Name\u00a0\":\"SDFSAD\",
                \"First Name\u00a0\":\"SDFAFSDA\",
                \"Middle Name\u00a0\":\"SAFDSAFD\",
                \"Gender\u00a0\":\"Male\"
            }"
        }

to

        {"AS_applicant_Data__c":"
            "{
                "Last Name":"SDFSAD",
                "First Name":"SDFAFSDA",
                "Middle Name":"SAFDSAFD",
                "Gender":"Male"
            }"
        }
  • 写回答

2条回答 默认 最新

  • douzhan5058 2015-12-03 07:38
    关注

    I use this code and it looks ok to me:

    <?php
    
    $string = <<<EOD
    {"AS_applicant_Data__c":
        "{
            \"Last Name\u00a0\":\"SDFSAD\",
            \"First Name\u00a0\":\"SDFAFSDA\",
            \"Middle Name\u00a0\":\"SAFDSAFD\",
            \"Gender\u00a0\":\"Male\"
        }"
    }
    EOD;
    $pattern = '#\\\\u[0-9a-f]{4}#i';
    $replacement = '';
    echo preg_replace($pattern, $replacement, $string);
    
    ?>
    

    This regular expression is blindly replace Unicode char in a format of \uxxxx with empty string. If you know for certain that there's only \u00a0, you can change regex to #\\\\u00a0#i

    Try a quick run here: http://ideone.com/xW4zTN

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100