preg_match_gf()

Run a regular expression against a string and return a single capture group — a one-liner for "match and extract" without handling a matches array yourself.

Syntax

preg_match_gf(pattern, subject, index)

Parameters

Name Type Required Description
pattern string yes The regex pattern (e.g. '/order-(\d+)/').
subject string yes The string to search.
index int yes Which group to return: 0 = whole match, 1 = first capture group, etc.

Returns

The matched group as a string, or an empty string "" if there's no match.

Example

$id = preg_match_gf('/invoice #(\d+)/i', $record["subject"], 1);
if ( $id != "" ) sys_log("Invoice number: " . $id);

Example output

"4471"

Notes

  • Returns "" (not false/null) when nothing matches — test with != "".
  • For all matches of a group, use preg_match_all_gf(). Standard preg_match() / preg_replace() are also available.

See also: preg_match_all_gf()