拆分字符串的最佳方法是什么

时间:2013-09-06 22:47:52

标签: php

我有这个字符串集

Host: example.com, IP address: 37.0.122.151, SBL: SBL196170, status: unknown, level: 4, Malware: Citadel, AS: 198310, country: RU

我想以这种格式提供每个数据。

$host = "example.com";
$ip = "37.0.122.151";
$SBL = "SBL196170";
$status = unknown;
$level = "4";
$malware = "Citadel";
$as = "1098310";
$country = "RU";

获得该字符串的最佳方法是什么?我应该先","":"之后再分开,还是有一个分裂的解决方案?

提前谢谢。

5 个答案:

答案 0 :(得分:4)

像这样:

$input = "Host: example.com, IP address: 37.0.122.151, SBL: SBL196170, status: unknown, level: 4, Malware: Citadel, AS: 198310, country: RU";
preg_match_all('/(\w+): ([\w.]+)/', $input, $matches);
print_r($matches);

输出:

Array
(
    [0] => Array
        (
            [0] => Host: example.com
            [1] => address: 37.0.122.151
            [2] => SBL: SBL196170
            [3] => status: unknown
            [4] => level: 4
            [5] => Malware: Citadel
            [6] => AS: 198310
            [7] => country: RU
        )

    [1] => Array
        (
            [0] => Host
            [1] => address
            [2] => SBL
            [3] => status
            [4] => level
            [5] => Malware
            [6] => AS
            [7] => country
        )

    [2] => Array
        (
            [0] => example.com
            [1] => 37.0.122.151
            [2] => SBL196170
            [3] => unknown
            [4] => 4
            [5] => Citadel
            [6] => 198310
            [7] => RU
        )

)

然后:

$mydata = array_combine($matches[1], $matches[2]);
print_r($mydata);

给出:

Array
(
    [Host] => example.com
    [address] => 37.0.122.151
    [SBL] => SBL196170
    [status] => unknown
    [level] => 4
    [Malware] => Citadel
    [AS] => 198310
    [country] => RU
)

答案 1 :(得分:1)

我会在字符串上使用简单的爆炸,然后为每个元素填充一个包含键/值信息的数组:

$string = 'Host: ...';
$raw_array = explode(',', $string);
$final_array = array();
foreach($raw_array as $item) {
    $item_array = explode(':', trim($item));
    $key = trim($item_array[0]);
    $value = trim($item_array[1]);
    $final_array[$key] = $value;
}
var_dump($final_array);

请注意,这不是在您的问题中使用像ask那样的单个变量,而是使用基于字符串键的键值填充单个数组。这是一种更灵活的方法。

答案 2 :(得分:1)

您可以使用正则表达式替换将其转换为查询字符串-esq字符串,然后使用parse_str将其转换为关联数组。没有循环,两行!

$string = preg_replace(array('/:/', '/, /'), array('=','&'), $string);
parse_str($string, $output);

var_dump($output);
/*
array(8) { ["Host"]=> string(8) " xxx.com" ["IP_address"]=> string(13) " 37.0.122.151" ["SBL"]=> string(10) " SBL196170" ["status"]=> string(8) " unknown" ["level"]=> string(2) " 4" ["Malware"]=> string(8) " Citadel" ["AS"]=> string(7) " 198310" ["country"]=> string(3) " RU" } 
*/

在此处试试:http://codepad.viper-7.com/5gwWyC

<强>文档

答案 3 :(得分:0)

在混合中加入一些函数式编程,你得到:

$string = 'Host: xxx.com, IP address: 37.0.122.151, SBL: SBL196170, status: unknown, level: 4, Malware: Citadel, AS: 198310, country: RU';
$result = array_reduce(explode(',', $string), function($result, $item) {
    $pair = explode(':', $item);
    $result[trim($pair[0])] = trim($pair[1]);
    return $result;
}, array());

答案 4 :(得分:0)

这是一个非常简单和方便的功能,我一直用这样的东西。

<?php

function get_string_between($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}

$src = "Host: xxx.com, IP address: 37.0.122.151, SBL: SBL196170, status: unknown, level: 4, Malware: Citadel, AS: 198310, country: RU";

//add a character to src to help identify the last field
$src = $src.",";

$host = get_string_between($src, "Host: ", ","); //this is grabbing any text between "Host: " and ","
$ip = get_string_between($src, "IP address: ", ",");
$SBL = get_string_between($src, "SBL: ", ",");
$status = get_string_between($src, "status: ", ",");
$level = get_string_between($src, "level: ", ",");
$malware = get_string_between($src, "Malware: ", ",");
$as = get_string_between($src, "AS: ", ",");
$country = get_string_between($src, "country: ", ",");

?>

快乐的编码!