如何使用PHP从字符串中查找特定字符以创建数组

时间:2018-11-23 04:53:32

标签: php arrays php-7.2

我有一些类似下面的字符串:

'STRIPLED 9.6W 24V 2.7K 120LED IP20 1CM',
'STRIPLED 9.6W 24V 3K 120LED IP20 1CM',
'STRIPLED H 6 W 24V 4K 140 LED IP20 1CM',
'STRIPLED HO 12.3 W 24V 3K 128 LED IP65 1CM',
'STRIPLED HO 20W 24V 2.7K 280 LED IP65 1CM',
'STRIPLED 14.4W 24V 2.7K 180 LED IP65 1 CM',

我试图找到一些字符并创建像这样的数组

预期结果:

Array
(
    [0] => Array
        (
            [power] => 9.6W 24V
            [temp] => 2.7
            [degrees] => IP20
        )

    [1] => Array
        (
            [power] => 9.6W 24V
            [temp] => 3
            [degrees] => IP20
        )
    [2] => Array
        (
            [power] => HO 12.3 W 24V
            [temp] => 3
            [degrees] => IP65
        )
)

但是我无法找到正确的解决方案。

  

说明:STRIPLED 9.6W 24V 2.7K 120LED IP20 1CM

这里,W&V表示功率,K表示温度,IP表示度数


我尝试过的代码:

<?php 
$descriptions = [
    'STRIPLED 9.6W 24V 2.7K 120LED IP20 1CM',
    'STRIPLED 9.6W 24V 3K 120LED IP20 1CM',
    'STRIPLED H 6 W 24V 4K 140 LED IP20 1CM',
    'STRIPLED HO 12.3 W 24V 3K 128 LED IP65 1CM',
    'STRIPLED HO 20W 24V 2.7K 280 LED IP65 1CM',
    'STRIPLED 14.4W 24V 2.7K 180 LED IP65 1 CM',
]; 

$desc = [];
foreach($descriptions as $key=>$val){
    $desc[$key]['power'] = strstr($val, 'V', true);
    $desc[$key]['temp'] = strstr($val, 'K', true);
    $desc[$key]['degrees'] = strstr($val, 'IP', true);
}

echo "<pre>";
print_r($desc).exit;
?>

我看到这样的输出:

Array
(
    [0] => Array
        (
            [power] => STRIPLED 9,6W 24
            [temp] => STRIPLED 9,6W 24V 2,7
            [degrees] => STR
        )

    [1] => Array
        (
            [power] => STRIPLED 9,6W 24
            [temp] => STRIPLED 9,6W 24V 3
            [degrees] => STR
        )

    [2] => Array
        (
            [power] => STRIPLED H 6 W 24
            [temp] => STRIPLED H 6 W 24V 4
            [degrees] => STR
        )

    [3] => Array
        (
            [power] => STRIPLED HO 12,3 W 24
            [temp] => STRIPLED HO 12,3 W 24V 3
            [degrees] => STR
        )

    [4] => Array
        (
            [power] => STRIPLED HO 20W 24
            [temp] => STRIPLED HO 20W 24V 2,7
            [degrees] => STR
        )

    [5] => Array
        (
            [power] => STRIPLED 14,4W 24
            [temp] => STRIPLED 14,4W 24V 2,7
            [degrees] => STR
        )

)

任何人都可以给我解决方案以创建预期的结果。

谢谢

2 个答案:

答案 0 :(得分:3)

我认为使用preg_match可能是提取所需数据的最简单方法。根据您的样本数据,此正则表达式应该起作用:

^\w+\s+([^V]+V)\s+([\d.,]+)K.*?(IP\d+)

这会寻找一个单词,然后是一些非V字符(“电源”部分),然后是K(“温度”部分)之前的数字,最后是一个匹配IP的字符串,后跟一些数字(“度”部分)。您可以使用以下代码(使用array_map来处理数组):

$descriptions = [
    'STRIPLED 9.6W 24V 2.7K 120LED IP20 1CM',
    'STRIPLED 9.6W 24V 3K 120LED IP20 1CM',
    'STRIPLED H 6 W 24V 4K 140 LED IP20 1CM',
    'STRIPLED HO 12.3 W 24V 3K 128 LED IP65 1CM',
    'STRIPLED HO 20W 24V 2.7K 280 LED IP65 1CM',
    'STRIPLED 14.4W 24V 2.7K 180 LED IP65 1 CM',
]; 
$desc = array_map(function ($v) { 
            preg_match('/^\w+\s+([^V]+V)\s+([\d.,]+)K.*?(IP\d+)/', $v, $m);
            array_shift($m); 
            return $m; }, $descriptions);
print_r($desc);

输出示例数据:

Array
(
    [0] => Array
        (
            [0] => 9.6W 24V
            [1] => 2.7
            [2] => IP20
        )    
    [1] => Array
        (
            [0] => 9.6W 24V
            [1] => 3
            [2] => IP20
        )    
    [2] => Array
        (
            [0] => H 6 W 24V
            [1] => 4
            [2] => IP20
        )    
    [3] => Array
        (
            [0] => HO 12.3 W 24V
            [1] => 3
            [2] => IP65
        )    
    [4] => Array
        (
            [0] => HO 20W 24V
            [1] => 2.7
            [2] => IP65
        )    
    [5] => Array
        (
            [0] => 14.4W 24V
            [1] => 2.7
            [2] => IP65
        )    
)

Demo on 3v4l.org

答案 1 :(得分:1)

您也可以尝试以下方法:

<?php
$arrays = [
    'STRIPLED 9.6W 24V 2.7K 120LED IP20 1CM',
    'STRIPLED 9.6W 24V 3K 120LED IP20 1CM',
    'STRIPLED H 6 W 24V 4K 140 LED IP20 1CM',
    'STRIPLED HO 12.3 W 24V 3K 128 LED IP65 1CM',
    'STRIPLED HO 20W 24V 2.7K 280 LED IP65 1CM',
    'STRIPLED 14.4W 24V 2.7K 180 LED IP65 1 CM'
];

foreach ($arrays as $array){
    //remove STRIPLED
    $result = [];
    $removedStripled = str_replace('STRIPLED ', "", $array);
    $arr = strtok($removedStripled, 'V');
    $result['power'] = $arr.'V';
    $temp = str_replace($arr.'V', "", $removedStripled);
    $arr = strtok($temp, 'K');
    $result['temp'] = $arr;
    preg_match("/IP.*[0-9\s]/", $removedStripled, $match);
    $degrees = explode(' ', $match[0]);
    $result['degress'] = $degrees[0];

    echo "<pre>";
    print_r($result);
    echo "</pre>";
}