3

我有这个字符串集

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";

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

先感谢您。

4

5 回答 5

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
)
于 2013-09-06T22:57:20.947 回答
1

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

$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);

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

于 2013-09-06T22:54:09.867 回答
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

文档

于 2013-09-06T23:04:03.900 回答
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());
于 2013-09-06T22:58:41.367 回答
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: ", ",");

?>

快乐编码!

于 2013-09-07T04:41:18.867 回答