dongzhan9100 2015-06-06 13:32
浏览 114
已采纳

php将此语法转换为object / array

Is there a way of turning this:

network={
    ssid="tele2-ssid-66577"
    #psk="testtest2"
    psk=8308d8e34c60fe471fda6837ab5821694e8cf51a655f24295797df33d02df6e9
}

into an object or an array with php?

I tried json_decode with no result.

------------- UPDATE:

the end goal is to extract only the psk key, I just thought turning it into an object/array would be the easiest thing to do rather than meddling with regular expressions or fiddling with the string, but maybe it's not possible...

  • 写回答

1条回答 默认 最新

  • dqppv86022 2015-06-06 14:07
    关注

    I ran it through regex and then some processing

    $str = 'network={
        ssid="tele2-ssid-66577"
        #psk="testtest2"
        psk=8308d8e34c60fe471fda6837ab5821694e8cf51a655f24295797df33d02df6e9
    }';
    
    $output = array();
    if (preg_match_all('/(([^=]+)=\{|\s+([^#]+)=(.*))/', $str, $match) and sizeof($match[2]) > 2) {
        $output[$match[2][0]] = array();
        for ($i=1; $i<sizeof($match[2]); $i++) {
            $key = trim($match[3][$i]);
            $value = trim($match[4][$i]);
    
            // remove outer double quotes
            if (preg_match('/^"(.*)"$/', $value, $match2)) $value = $match2[1];
    
            // save
            $output[$match[2][0]][$key] = $value;
        }
    }
    
    print_r($output);
    

    Giving the output:

    Array
    (
        [network] => Array
            (
                [ssid] => tele2-ssid-66577
                [psk] => 8308d8e34c60fe471fda6837ab5821694e8cf51a655f24295797df33d02df6e9
            )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)