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 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘