Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按空格将字符串转换为数组_Php_Arrays - Fatal编程技术网

Php 按空格将字符串转换为数组

Php 按空格将字符串转换为数组,php,arrays,Php,Arrays,我有下面的示例字符串 device_name="Text Data" d_id=7454579598 status="Active" Key=947-4378-43248274 我想把这个字符串转换成数组 预期产量 device_name="Text Data" d_id=7454579598 status="Active" Key=947-4378-43248274 device_name="Text Data" d_id=7454579598 status="Active" Key

我有下面的示例字符串

device_name="Text Data" d_id=7454579598 status="Active" Key=947-4378-43248274
我想把这个字符串转换成数组

预期产量

device_name="Text Data"
d_id=7454579598
status="Active" 
Key=947-4378-43248274
device_name="Text 
Data"
d_id=7454579598
status="Active" 
Key=947-4378-43248274
我尝试了这个使用爆炸函数,但它给出了下面的输出

$data='device_name="Text Data" d_id=7454579598 status="Active" Key=947-4378-43248274';
$arr= explode("",$data);
生成输出

device_name="Text Data"
d_id=7454579598
status="Active" 
Key=947-4378-43248274
device_name="Text 
Data"
d_id=7454579598
status="Active" 
Key=947-4378-43248274
试试这个代码

    $str = 'device_name="Text Data" d_id=7454579598 status="Active" Key=947-4378-43248274';
$pattern = '/(\\w+)\s*=\\s*("[^"]*"|\'[^\']*\'|[^"\'\\s>]*)/';
preg_match_all($pattern, $str, $matches, PREG_SET_ORDER);

$attrs = array();
foreach ($matches as $match) {
    if (($match[2][0] == '"' || $match[2][0] == "'") && $match[2][0] == $match[2][strlen($match[2])-1]) {
        $match[2] = substr($match[2], 1, -1);
    }
    $name = strtolower($match[1]);
    $value = html_entity_decode($match[2]);
    $attrs[$name] = $value;
}
print_r($attrs);
输出

Array ( [device_name] => Text Data [d_id] => 7454579598 [status] => Active [key] => 947-4378-43248274 ) 
试试这个代码

    $str = 'device_name="Text Data" d_id=7454579598 status="Active" Key=947-4378-43248274';
$pattern = '/(\\w+)\s*=\\s*("[^"]*"|\'[^\']*\'|[^"\'\\s>]*)/';
preg_match_all($pattern, $str, $matches, PREG_SET_ORDER);

$attrs = array();
foreach ($matches as $match) {
    if (($match[2][0] == '"' || $match[2][0] == "'") && $match[2][0] == $match[2][strlen($match[2])-1]) {
        $match[2] = substr($match[2], 1, -1);
    }
    $name = strtolower($match[1]);
    $value = html_entity_decode($match[2]);
    $attrs[$name] = $value;
}
print_r($attrs);
输出

Array ( [device_name] => Text Data [d_id] => 7454579598 [status] => Active [key] => 947-4378-43248274 ) 

考虑这个使用正则表达式的小而平凡的示例:

<?php    
$subject = 'device_name="Text Data" d_id=7454579598 status="Active" Key=947-4378-43248274';
$pattern = '/^device_name="(.*)" d_id=(\d+) status="(.*)" Key=([0-9-]*)$/';
preg_match($pattern, $subject, $tokens);
var_dump($tokens);

从这里,您可能可以自己继续:-)

考虑这个使用正则表达式的小而琐碎的示例:

<?php    
$subject = 'device_name="Text Data" d_id=7454579598 status="Active" Key=947-4378-43248274';
$pattern = '/^device_name="(.*)" d_id=(\d+) status="(.*)" Key=([0-9-]*)$/';
preg_match($pattern, $subject, $tokens);
var_dump($tokens);

从这里,您可能可以自己继续:-)

首先将其转换为JSON

获取JSON。比如:

{device_name:"Text Data",d_id:7454579598,status:"Active",Key:947-4378-43248274}
并转换为数组

$array = json_decode($json, true);
和输出

array(4) {
    ["device_name"] => "Text Data"
    ["d_id"] => 7454579598
    ["status"] => "Active"
    ["Key"] => "947-4378-43248274"
}

首先转换为JSON

获取JSON。比如:

{device_name:"Text Data",d_id:7454579598,status:"Active",Key:947-4378-43248274}
并转换为数组

$array = json_decode($json, true);
和输出

array(4) {
    ["device_name"] => "Text Data"
    ["d_id"] => 7454579598
    ["status"] => "Active"
    ["Key"] => "947-4378-43248274"
}

explode函数会将数据转换为数组。是的,但有一个问题。使用该函数,它也会分解值。您需要像$array['device_name']@Jawlon Rodriguez Yestack看一看正则表达式。它们实际上很有趣:-)explode函数会将数据转换为数组。是的,但有一个问题。使用它,它也会分解值。您需要像$array['device_name']@Jawlon Rodriguez Yestack看一看正则表达式。它们实际上很有趣:-)如何动态地进行?这只是示例数据。好的,您在问题中没有提到这一点。我假设您有一个要处理的固定格式。对于动态匹配,模式显然变得更加复杂。看看上面贴的答案@Karan。看起来不错:-)如何动态执行?这只是示例数据。好的,您在问题中没有提到这一点。我假设您有一个要处理的固定格式。对于动态匹配,模式显然变得更加复杂。看看上面贴的答案@Karan。看起来不错:-)