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


what is the best way to split the string

我设置了这个字符串

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

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

提前谢谢你。

像这样:

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

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

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

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

您可以使用

正则表达式替换将其转换为查询字符串 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

文档

  • preg_replace - http://php.net/manual/en/function.preg-replace.php
  • parse_str - http://php.net/manual/en/function.parse-str.php

一些函数式编程放入其中,您可以得到:

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

这是一个非常简单方便的函数,我一直在做这样的事情。

<?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: ", ",");
?>

祝您编码愉快!