preg_match_all()
preg_match_all() finds all matches and capture groups, returning the count. The matches are stored in the passed array.
preg_match_all() finds all matches and capture groups, returning the count. The matches are stored in the passed array.
<?php
$html = "<a href=\"page1.html\">Link 1</a> <a href=\"page2.html\">Link 2</a>";
$count = preg_match_all("/<a href=\"([^\"]+)\">([^<]+)<\/a>/", $html, $matches);
echo "Found {$count} links
";
print_r($matches[1]); // ["page1.html", "page2.html"]
print_r($matches[2]); // ["Link 1", "Link 2"]
// $matches[0] = full matches
// $matches[1] = first capture group
// $matches[2] = second capture group
$matches[0] contains full matches; $matches[1] is the first capture group — remember this ordering.