如何使用 PHP 从字符串中提取有用信息
How to extract useful information from a string with PHP
我正在使用按以下方式组成的字符串:
0003customer-23892644362977289 28 23892644362977293 29 23892644362977294 30
前四个字符:要处理的注册表数。
String "customer-": 定义要使用的table
链的下一部分定义为字符串的键 => 值(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>';
}
但循环仅适用于单个字符值对。
由于缺乏代表性,这只是推测。
这是我想出的:
<?
$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
)
)
编辑:
删除了每个 explode()
上的 $times * 2
,以防输入为 0001customer-23892644362977289 28 23892644362977293 29 23892644362977294 30
(注意 0001
)。
瞧。
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
这可以通过单个正则表达式实现。
(?:^(.{4})([^-]*)|\G)[\s-](\S+)\s+(\S+)
$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]);
我不擅长正则表达式,所以我喜欢编写常规 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
)
我正在使用按以下方式组成的字符串:
0003customer-23892644362977289 28 23892644362977293 29 23892644362977294 30
前四个字符:要处理的注册表数。
String "customer-": 定义要使用的table
链的下一部分定义为字符串的键 => 值(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>';
}
但循环仅适用于单个字符值对。
由于缺乏代表性,这只是推测。
这是我想出的:
<?
$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
)
)
编辑:
删除了每个 explode()
上的 $times * 2
,以防输入为 0001customer-23892644362977289 28 23892644362977293 29 23892644362977294 30
(注意 0001
)。
瞧。
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
这可以通过单个正则表达式实现。
(?:^(.{4})([^-]*)|\G)[\s-](\S+)\s+(\S+)
$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]);
我不擅长正则表达式,所以我喜欢编写常规 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
)