【问题标题】:Is there way to get value of CDATA and convert in json or array format in php?有没有办法获取 CDATA 的值并在 php 中转换为 json 或数组格式?
【发布时间】:2021-12-03 05:50:17
【问题描述】:

这是通过 simplexmlelement 从 CDATA 获取值后的样子

$data = "{customertype=New, Telephone=09832354544, CITY=Henfield, LASTNAME=C, TicketNo=123456, FIRSTNAME=Alex, Id=10001273, testfield=123456, COMPANY=Camp1}"

我尝试在谷歌中寻找解决方案,但我找不到将这组字符串转换为数组的解决方案。

我希望将这些数据转换成类似这样的数组

["customertype"] = ["New"]
["Telephone"] = ["09832354544"]

诸如此类或类似于数组的外观。 提前致谢

【问题讨论】:

    标签: php regex laravel cdata


    【解决方案1】:

    根据您的数据字符串格式,您可以执行以下操作:
    首先,去掉括号{ }
    然后使用, 分隔符分解字符串。
    现在,您拥有包含例如customertype=New 的字符串数组。
    接下来是棘手的部分,结果字符串看起来像一个查询,所以我们要使用 parse_str() 制作关联数组:

    结果会是这样的:

    array:9 [▼
      0 => array:1 [▼
        "customertype" => "New"
      ]
      1 => array:1 [▼
        "Telephone" => "09832354544"
      ]
      2 => array:1 [▼
        "CITY" => "Henfield"
      ]
    

    这是代码:

        $data = "{customertype=New, Telephone=09832354544, CITY=Henfield, LASTNAME=C, TicketNo=123456, FIRSTNAME=Alex, Id=10001273, testfield=123456, COMPANY=Camp1}";
    
        $rippedData = str_replace(["{", "}"],"", $data);
    
        $singleData= explode(",", $rippedData);
    
        $finalArray = [];
        foreach($singleData as $string){
            parse_str($string, $output);
            $finalArray[] = $output;
        }
    
    

    【讨论】:

    • 谢谢你的作品。我没有足够的声誉来喜欢这条评论,但我真的很感谢你的帮助
    【解决方案2】:
    $data = "{customertype=New, Telephone=09832354544, CITY=Henfield, LASTNAME=C, TicketNo=123456, FIRSTNAME=Alex, Id=10001273, testfield=123456, COMPANY=Camp1}";
    
    $stripped = str_replace(['{','}'], '', $data);
    $explode = explode(', ', $stripped);
    
    $output = [];
    
    foreach ($explode as $value) {
        $inner_explode = explode('=', $value);
        $output[$inner_explode[0]] = $inner_explode[1];
    }
    
    var_dump($output);
    

    结果

    array(9) {
        ["customertype"]=>
        string(3) "New"
        ["Telephone"]=>
        string(11) "09832354544"
        ["CITY"]=>
        string(8) "Henfield"
        ["LASTNAME"]=>
        string(1) "C"
        ["TicketNo"]=>
        string(6) "123456"
        ["FIRSTNAME"]=>
        string(4) "Alex"
        ["Id"]=>
        string(8) "10001273"
        ["testfield"]=>
        string(6) "123456"
        ["COMPANY"]=>
        string(5) "Camp1"
      }
    

    【讨论】:

    • 谢谢你的作品。我没有足够的声誉来喜欢这条评论,但我真的很感谢你的帮助。
    【解决方案3】:

    这应该可以工作,尽管可能有更好/更简洁的编码方式。

    $data = "{customertype=New, Telephone=09832354544, CITY=Henfield, LASTNAME=C, TicketNo=123456, FIRSTNAME=Alex, Id=10001273, testfield=123456, COMPANY=Camp1}";
    
    // Replace squiggles
    $replace_this = array("{","}");
    $replace_data = str_replace($replace_this,"",$data);
    
    // Explode and create array
    $data_array = explode(',',$replace_data);
    
    // Loop through the data_array
    foreach($data_array as $value) {
        // Explode the value on the equal sign
        $explode_again = explode('=', $value);
    
        // Create new array with correct indexes and values
        $CDTA[trim($explode_again[0])] = $explode_again[1];
    }
    echo '<pre>' . var_export($CDTA, true) . '</pre>';
    

    结果:

    array (
      'customertype' => 'New',
      'Telephone' => '09832354544',
      'CITY' => 'Henfield',
      'LASTNAME' => 'C',
      'TicketNo' => '123456',
      'FIRSTNAME' => 'Alex',
      'Id' => '10001273',
      'testfield' => '123456',
      'COMPANY' => 'Camp1',
    )
    

    【讨论】:

    • 谢谢你的作品。我没有足够的声誉来喜欢这个评论,但我真的很感谢你的帮助
    猜你喜欢
    • 2012-05-03
    • 2019-12-16
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 2022-01-15
    • 2019-08-22
    相关资源
    最近更新 更多