I did not understand exactly what you want, because it depends on how many groups will be captured.
I made a function to capture the offset of the last capture according to the group number. In my pattern, I have three groups: the first group, full capture and the other two groups, sub-groups.
Pattern sample code:
$pattern = "/<a[^\x3e]{0,}href=\x22([^\x22]*)\x22>([^\x3c]*)<\/a>/";
HTML sample code:
$subject = '<ul>
<li>Search Engines</li>
<li><a href="https://www.google.com/">Google</a></li>
<li><a href="http://www.bing.com/">Bing</a></li>
<li><a href="https://duckduckgo.com/">DuckDuckGo</a></li>
</ul>';
My function captures the offset of the last element and you have the possibility to indicate the number of matching:
function get_offset_last_match( $pattern, $subject, $number ) {
if ( preg_match_all( $pattern, $subject, $matches, PREG_OFFSET_CAPTURE ) == false ) {
return false;
}
return $matches[$number][count( $matches[0] ) - 1][1];
}
You can get detailed information about preg_match_all here on official documentation.
Using my pattern for example:
0 => all text
1 => href value
2 => innerHTML
echo '<pre>';
echo get_offset_last_match( $pattern, $subject, 0 ) . PHP_EOL; // all text
echo get_offset_last_match( $pattern, $subject, 1 ) . PHP_EOL; // href value
echo get_offset_last_match( $pattern, $subject, 2 ) . PHP_EOL; // innerHTML
echo '</pre>';
die();
The output is:
140
149
174
My function (text):
function get_text_last_match( $pattern, $subject, $number ) {
if ( preg_match_all( $pattern, $subject, $matches, PREG_OFFSET_CAPTURE ) == false ) {
return false;
}
return $matches[$number][count( $matches[0] ) - 1][0];
}
Sample code:
echo '<textarea style="font-family: Consolas: font-size: 14px; height: 200px; tab-size: 4; width: 90%;">';
echo 'ALL = ' . get_text_last_match( $pattern, $subject, 0 ) . PHP_EOL; // all text
echo 'HREF = ' . get_text_last_match( $pattern, $subject, 1 ) . PHP_EOL; // href value
echo 'INNER = ' . get_text_last_match( $pattern, $subject, 2 ) . PHP_EOL; // innerHTML
echo '</textarea>';
The output is:
ALL = <a href="https://duckduckgo.com/">DuckDuckGo</a>
HREF = https://duckduckgo.com/
INNER = DuckDuckGo