从字符串中提取数字 - 为什么在使用捕获组时会得到两个数组?
Extracting numbers from string - Why do I get two arrays when using a capture group?
我正在尝试从混合字符串中提取数字。
<?php
$string = "c <a data-player-id=\"5528\" href=\"/players/5528-ga-name--5406546\" target=\"_self\">GA Name</a> b <a data-player-id=\"8992842\" href=\"/players/8992842-chandran-win--123345\" target=\"_self\">C Win</a>";
//preg_match_all('!\d+!', $string, $matches);
//preg_match_all('/data-player-id=\"(\d+)/', $string, $matches);
preg_match_all('/\/players\/(\d+)/', $string, $matches);
print_r($matches);
?>
但结果是 2 个数组:
Array
(
[0] => Array
(
[0] => /players/5528
[1] => /players/8992842
)
[1] => Array
(
[0] => 5528
[1] => 8992842
)
)
我想捕获 5528
和 8992842
这样的数字。以下代码无效。
/*
$zero = $matches[0];
$one = $matches[1];
$two = $matches[2];
echo $zero;
echo $one;
echo $two;
*/
编辑:
知道为什么它 returns 变成 2 个数组吗?
是否可以计算 array[1]
中的项目?
您可以使用 foreach
循环打印在 $matches[1]
中找到的所有值
尝试
$string = "c <a data-player-id=\"5528\" href=\"/players/5528-ga-name--5406546\" target=\"_self\">GA Name</a> b <a data-player-id=\"8992842\" href=\"/players/8992842-chandran-win--123345\" target=\"_self\">C Win</a>";
preg_match_all('/\/players\/(\d+)/', $string, $matches);
//print_r($matches);
foreach($matches[1] as $match)
{
echo $match."<br />";
}
更新 1
是的,您可以使用 count()
计算在 $matches[1]
中找到的元素
$total_matches = count($matches[1]);
echo $total_matches;
尝试这样的事情。
<?php
$string = "c <a data-player-id=\"5528\" href=\"/players/5528-ga-name--5406546\" target=\"_self\">GA Name</a> b <a data-player-id=\"8992842\" href=\"/players/8992842-chandran-win--123345\" target=\"_self\">C Win</a>";
preg_match_all('!\d+!', $string, $matches);
$arr = array_unique($matches[0]);
// For Count items...
$count = count($arr);
echo $count;
foreach($arr as $match)
{
echo $match."<br />";
}
?>
输出
5528
5406546
8992842
123345
我正在尝试从混合字符串中提取数字。
<?php
$string = "c <a data-player-id=\"5528\" href=\"/players/5528-ga-name--5406546\" target=\"_self\">GA Name</a> b <a data-player-id=\"8992842\" href=\"/players/8992842-chandran-win--123345\" target=\"_self\">C Win</a>";
//preg_match_all('!\d+!', $string, $matches);
//preg_match_all('/data-player-id=\"(\d+)/', $string, $matches);
preg_match_all('/\/players\/(\d+)/', $string, $matches);
print_r($matches);
?>
但结果是 2 个数组:
Array
(
[0] => Array
(
[0] => /players/5528
[1] => /players/8992842
)
[1] => Array
(
[0] => 5528
[1] => 8992842
)
)
我想捕获 5528
和 8992842
这样的数字。以下代码无效。
/*
$zero = $matches[0];
$one = $matches[1];
$two = $matches[2];
echo $zero;
echo $one;
echo $two;
*/
编辑:
知道为什么它 returns 变成 2 个数组吗?
是否可以计算 array[1]
中的项目?
您可以使用 foreach
循环打印在 $matches[1]
尝试
$string = "c <a data-player-id=\"5528\" href=\"/players/5528-ga-name--5406546\" target=\"_self\">GA Name</a> b <a data-player-id=\"8992842\" href=\"/players/8992842-chandran-win--123345\" target=\"_self\">C Win</a>";
preg_match_all('/\/players\/(\d+)/', $string, $matches);
//print_r($matches);
foreach($matches[1] as $match)
{
echo $match."<br />";
}
更新 1
是的,您可以使用 count()
$matches[1]
中找到的元素
$total_matches = count($matches[1]);
echo $total_matches;
尝试这样的事情。
<?php
$string = "c <a data-player-id=\"5528\" href=\"/players/5528-ga-name--5406546\" target=\"_self\">GA Name</a> b <a data-player-id=\"8992842\" href=\"/players/8992842-chandran-win--123345\" target=\"_self\">C Win</a>";
preg_match_all('!\d+!', $string, $matches);
$arr = array_unique($matches[0]);
// For Count items...
$count = count($arr);
echo $count;
foreach($arr as $match)
{
echo $match."<br />";
}
?>
输出
5528
5406546
8992842
123345