PHP preg_match 和 html 属性,数字用逗号分隔

PHP preg_match an html arttribue with number seprated by a comma

我正在尝试获取属性的值并将它们填充到数组中:

<p numbers="11.2,1.1 2,3,3.1 33"></p>

preg_match('/numbers="([0-9\.]]+)"/',$elements,$match);  

我需要这样的东西:

$match[0] -> 11.2
$match[1] -> 1.1
$match[2] -> 2

或者任何可以让我将数字存储在数组中的方法,但我无法找出它的正则表达式。

就我个人而言,我会简单地匹配所有数字然后拆分它们

$matches = preg_match('/numbers="(.*)"/',$elements,$match);
$numbers = explode(',', $matches[1]);

我想我有一个解决方案:

$string = '<tag> etc</tag>
<p numbers="11.2,1.1 2,3,3.1 33"></p>
<div> etc</div>';

// Catch only the numerals from the attribute numbers
preg_match('/<p.*numbers="(.*)"/', $string, $match);

// Catch each number separated by comma or space.
preg_match_all('/(\d+(\.?|\s?)(\d+)?)/', $match[0], $matches);

echo '<pre>';
print_r($matches[0]);

正则表达式在某种程度上非常简单,但如果您需要一些说明,请发表评论。

您可以在此处查看它的测试结果:https://3v4l.org/iVp1v