preg_match和regex根据部分字符串选择完整字符串

I have an html that contains this markup.

<font class="count">Total count is: 20</font>

How do I use preg_match to get the total count line, which is 20 in this case.

This is simple:

$string = '<font class="count">Total count is: 20</font>';

preg_match('/Total count is:\s+(\d+)/', $string, $match);

echo $match[1]; // 20

Otherwise use a DOM method if you want to find the <font> tag within other HTML to extract the text portion of the font node and then grab the number at end of the node value.

Here is another way just for fun:

$string = '<font class="count">Total count is: 20</font>';

$string = filter_var($string, FILTER_SANITIZE_NUMBER_INT);

echo $string; // 20

And another:

$string = '<font class="count">Total count is: 20</font>';

$string = ltrim(strrchr(strip_tags($string), ' '));

echo $string; // 20

You can use

$foo = '<font class="count">Total count is: 20</font>';
preg_match('/<font class="count">Total count is: (\d+)</font>/', $foo, $matches);
echo $matches[1];

But it would be better to use a HTML parser to grab the contents of the html element, and then apply the regular expression to that.