dongyan1548 2011-04-04 20:06
浏览 43
已采纳

php将带有新行的字符串转换为数组?

I am getting data from an API and the resulting string is

[RESPONSE]
PROPERTY[STATUS][0]=ACTIVE
PROPERTY[REGISTRATIONEXPIRATIONDATE][0]=2012-04-04 19:48:48
DESCRIPTION=Command completed successfully
QUEUETIME=0
CODE=200
RUNTIME=0.352

QUEUETIME=0
RUNTIME=0.8

EOF

I am trying to convert this into an array like

Array(
['PROPERTY[STATUS][0]'] => ACTIVE,
['CODE'] => 200,
...
);

So I am trying to explode it using the resulting file_get_content function with an explode like

$output = explode('=',file_get_contents($url));

But the problem is the returning values are not always returned in the same order, so I need to have it like $array['CODE'] = 200, and $array['RUNTIME'] = 0.352 however there does not seem to be any kind of new line characters? I tried , , <br>, in the explode function to no avail. But there is new lines in both notepad and the browser.

So my question is there some way to determine if a string is on a new line or determine what the character forcing the new line is? If not is there some other way I could read this into an array?

  • 写回答

5条回答 默认 最新

  • dsa111111 2011-04-04 20:14
    关注

    To find out what the breaking character is, you could do this (if $data contatins the string example you've posted):

    echo ord($data[strlen('[RESPONSE]')]) . PHP_EOL;
    echo ord($data[strlen('[RESPONSE]')+1]);          // if there's a second char
    

    Then take a look in the ASCII table to see what it is.

    EDIT: Then you could explode the data using that newly found character:

    explode(ord($ascii_value), $data);
    

    Btw, does file() return a correct array?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来