dongxun5349 2017-11-20 20:52
浏览 36

使用php删除单词中的数字

I have a string that is returned from an API. For some reason one of the values within the JSON returned is null54 which breaks the json_decode and therefore doesn't allow me to access it as an array correctly.

Example String: {"ttl": null54 "card_id": "np"}.DPTQIg.eeQ-wZYaH8ptEofYGZM0E8ICZDQ

You will see this has null54 as a value for ttl. This prevents the below PHP from working. If I manually remove 54 then the php works fine. How can I remove any numbers that may be with null to prevent the php from not working? I cannot guarantee that it will always be 54 either.

Working PHP:

$string = '{"ttl": null, "card_id": "np"}.DPSXmw.VApKpbKiEnEoRgwWblgt-nuewFg';
$string = explode('}', $string);

$json = json_decode($string[0] .'}');
echo $json->card_id;

Non-Working PHP:

$string = '{"ttl": null54, "card_id": "np"}.DPSXmw.VApKpbKiEnEoRgwWblgt-nuewFg';
$string = explode('}', $string);

$json = json_decode($string[0] .'}');
echo $json->card_id;
  • 写回答

1条回答 默认 最新

  • dongwu9647 2017-11-20 20:59
    关注

    This is clearly a bug with the API, and @chris-forrence is correct in that you should raise a bug with the API maintainer if possible... That being said, you'll probably have to work around the issue, at least for a while.

    I think the easiest way to do this would be using a regexp find and replace. This can be handled with the preg_replace() function and would look something like:

    $string = preg_replace('/null[0-9]+/', 'null', $string)
    

    Here the first argument is a regex, that matches the string 'null' and any number of digits following it.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作