Non ho capito esattamente quello che vuoi, perché dipende da quanti gruppi verranno catturati, ho fatto una funzione per catturare l'offset dell'ultimo accordo di cattura al mio gruppo, nel mio modello, abbiamo 3 gruppi: il primo gruppo, la cattura completa e gli altri due gruppi, i sottogruppi.
modello codice di esempio:
$pattern = "/<a[^\x3e]{0,}href=\x22([^\x22]*)\x22>([^\x3c]*)<\/a>/";
codice HTML di esempio:
$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>';
mia funzione, si coglie l'offset dell'ultimo elemento e si ha la possibilità di indicare il numero di 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];
}
È possibile ottenere informazioni dettagliate su preg_match_all here sulla documentazione ufficiale.
Usando il mio modello, ad esempio:
0 => tutto il testo
1 => href valore
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();
uscita è:
140
149
174
mio funzione (testo):
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];
}
codice di esempio:
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>';
uscita è:
ALL = <a href="https://duckduckgo.com/">DuckDuckGo</a>
HREF = https://duckduckgo.com/
INNER = DuckDuckGo
Si prega di indicare chiaramente, *** HO QUESTO E ho bisogno di questa uscita ***, aiutaci ad aiutarvi. –