【问题标题】:How to extract useful information from a string with PHP如何使用 PHP 从字符串中提取有用信息
【发布时间】:2015-02-17 17:47:36
【问题描述】:

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

0003customer-23892644362977289 28 23892644362977293 29 23892644362977294 30

前四个字符:要处理的注册表数。

String "customer-":定义要使用的表

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

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

$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>';
}

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

【问题讨论】:

  • 提供预期的输出。
  • - 是否被用作表名中的分隔符?
  • 我需要提取前四个字符来定义循环的长度,我需要表名来知道要对哪个表进行操作,在表用作分隔符之后,之后我需要提取每个值对才能将它们插入到数组中
  • 我更新了代码以反映我正在使用的整个过程。如果需要,我还可以更新字符串结构。
  • 在分隔符上拆分,使用您当前的子字符串逻辑从split[0]获取注册表数量,然后在split[1]上拆分空间并修改您当前的for循环以使用最后一个拆分而不是$array.

标签: php regex string substring


【解决方案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

【讨论】:

  • 你会如何处理0001customer-23892644362977289 28 23892644362977293 29 23892644362977294 30
  • 正则表达式的第一部分处理所有数字(preg_match("#^([0-9]+))
【解决方案2】:

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

这是我想出的:

<?

$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=&gt;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
        )

)

编辑:

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

【讨论】:

  • 谢谢!感谢您的帮助。
  • @vicman 不客气。如果您需要我解释代码,以及您对哪位感兴趣,请询问,我会很乐意回答。
【解决方案3】:

我不擅长正则表达式,所以我喜欢编写常规的 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
)

【讨论】:

    【解决方案4】:

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

    (?:^(.{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]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-25
      • 2014-10-25
      • 2018-09-14
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 2022-01-13
      相关资源
      最近更新 更多