如何使用PHP从字符串中提取有用信息

时间:2015-02-17 17:47:37

标签: php regex string substring

我正在使用以下列方式组成的字符串:

0003customer-23892644362977289 28 23892644362977293 29 23892644362977294 30

前四个字符:要处理的注册管理机构数量。

字符串“customer-”:定义要使用的表

链的下一部分被定义为一个键=>字符串的值(uuid_short =>远程ID)

我需要将链的每个部分分开,以便可以在本地处理 我尝试使用子串,爆炸和以下循环:

$array = explode(" ", $string);

for($i = 0, $b = 0; $b < $num_of_registries; $i = $i + 2, $b++)
{
  $local = $array[$i];
  $remote = $array[$i+1];
  // store values in array

  $inserted = array();
  $inserted['local'] = $local; 
  $inserted['remote'] = $remote;
  $array_inserted[] = $inserted;
}

foreach ($array_inserted as $key => $value)
{
  echo $value["local"].' => '.$value["remote"].' <p>';
}

但循环仅适用于单字符值对。

4 个答案:

答案 0 :(得分:1)

由于缺乏代表性,这仅仅是推测性的。

这就是我提出的:

<?

$string='0003customer-23892644362977289 28 23892644362977293 29 23892644362977294 30';

$array = explode('-', $string);

$header = current( $array );

$times = (int)$header;

$data = array();

$data[$key = substr($header, 4)] = explode(' ', $array[1]);

print_r($data);

应输出:

Array
(
    [customer] => Array
        (
            [0] => 23892644362977289
            [1] => 28
            [2] => 23892644362977293
            [3] => 29
            [4] => 23892644362977294
            [5] => 30
        )

)

如果你想要key=>value,你可能需要这个:

$string='0003customer-23892644362977289 28 23892644362977293 29 23892644362977294 30';

$array = explode('-', $string);

$header = current( $array );

$times = (int)$header;
$key = substr($header, 4);

$data = array("$key"=>array());

$content = explode(' ', $array[1]);

for($i=0;$i < $times * 2;)
{
    $data[$key][$content[$i++]]=$content[$i++];
}

print_r($data);

打印哪些:

Array
(
    [customer] => Array
        (
            [23892644362977289] => 28
            [23892644362977293] => 29
            [23892644362977294] => 30
        )

)

或者这个:

$string='0003customer-23892644362977289 28 23892644362977293 29 23892644362977294 30';

$array = explode('-', $string);

$header = current( $array );

$times = (int)$header;
$key = substr($header, 4);

$data = array("$key"=>array());

$content = explode(' ', $array[1]);

for($i=0;$i < $times * 2;)
{
    $data[$key][$content[1+$i++]]=$content[$i++-1];
}

print_r($data);

打印哪些:

Array
(
    [customer] => Array
        (
            [28] => 23892644362977289
            [29] => 23892644362977293
            [30] => 23892644362977294
        )

)

修改

删除$times * 2上的explode(),以防输入为0001customer-23892644362977289 28 23892644362977293 29 23892644362977294 30(请注意0001)。

答案 1 :(得分:1)

瞧。

if (preg_match("#^([0-9]+)(.*?)-(.*)#", $string, $out)) {
    echo "number: ".$out[1]."\n";
    echo "table: ".$out[2]."\n";
    if (preg_match_all("#([0-9]+) ([0-9]+)#", $out[3], $res, PREG_SET_ORDER))
       foreach ($res as $r) echo "$r[1] => $r[2]\n";
}

结果

number: 0003
table: customer
23892644362977289 => 28
23892644362977293 => 29
23892644362977294 => 30

答案 2 :(得分:0)

这可以通过单个正则表达式实现。

(?:^(.{4})([^-]*)|\G)[\s-](\S+)\s+(\S+)

DEMO

$str = "0003customer-23892644362977289 28 23892644362977293 29 23892644362977294 30";
preg_match_all('~(?:^(.{4})([^-]*)|\G)[\s-](\S+)\s+(\S+)~', $str, $matches);
echo "Number : ".$matches[1][0]."\n";
echo "Table : ".$matches[2][0]."\n";
print_r($matches[3]);
print_r($matches[4]);

答案 3 :(得分:0)

我吮吸正则表达式,所以我喜欢编写常规的PHP代码,所以我知道发生了什么。你走了:

$input = "0003customer-23892644362977289 28 23892644362977293 29 23892644362977294 30";

$resistry_count = substr($input, 0, 4);  // example: '0003'
$table = substr($input, 4, strpos($input, '-') - 4); // example: 'customer'
$data = substr($input, strpos($input, '-') + 1); // example: '23892644362977289 28 23892644362977293 29 23892644362977294 30'

$exploded = explode(" ", $data);
$chunked = array_chunk($exploded, 2);

$array = [];
foreach ($chunked as $row) {
    $array[$row[0]] = $row[1];
}

,你的结果应该是:

Array
(
    [23892644362977289] => 28
    [23892644362977293] => 29
    [23892644362977294] => 30
)