preg_match_all_gf()

Run a regular expression that matches multiple times and return one occurrence of its first capture group.

Syntax

preg_match_all_gf(pattern, subject, index)

Parameters

Name Type Required Description
pattern string yes The regex pattern, with a capture group.
subject string yes The string to search.
index int yes Which occurrence of the first capture group to return — 1-based (1 = the first match).

Returns

The requested match as a string, or an empty string "" if there aren't that many matches.

Example

$text = "tags: #alpha #beta #gamma";
$second = preg_match_all_gf('/#(\w+)/', $text, 2);
sys_log($second);   // "beta"

Example output

"beta"

Notes

  • The index is 1-based and applies to the first capture group across all matches.
  • Returns "" when the occurrence doesn't exist.
  • For a single match, use preg_match_gf().

See also: preg_match_gf()