Skip to content

preg_match: the A modifier works with offset - #5887

Merged
lacatoire merged 1 commit into
php:masterfrom
KentarouTakeda:fix-preg-match-offset-a-modifier
Sep 27, 2026
Merged

lacatoire merged 1 commit into
php:masterfrom
KentarouTakeda:fix-preg-match-offset-a-modifier

Conversation

@KentarouTakeda

Copy link
Copy Markdown
Contributor

This reverts the wording change from #5652. The text introduced by that PR is:

use the \G assertion rather than the ^ anchor, or the A modifier; both of which do not work with the offset parameter.

The problem is that both includes the A modifier. It actually works like this:

$ php -a
Interactive shell

php > echo PHP_VERSION, " / PCRE ", PCRE_VERSION, PHP_EOL;
8.5.11 / PCRE 10.48 2026-08-31
php > var_dump(
php (   preg_match('/\Gdef/', "abcdef", $m, 0, 3),
php (   preg_match('/def/A',  "abcdef", $m, 0, 3), // works fine
php (   preg_match('/def/A',  "abcdef", $m, 0, 2), // anchored at offset 2: no match
php (   preg_match('/def/',   "abcdef", $m, 0, 2), // unanchored: matches
php (   preg_match('/^def/',  "abcdef", $m, 0, 3), // previous PR's scope
php (   preg_match('/\Adef/', "abcdef", $m, 0, 3), // previous PR's scope
php ( );
int(1)
int(1)
int(0)
int(1)
int(0)
int(0)

By the way, the original PR's description says:

It read as if ^ and \A both worked with offset and to use use \A instead of ^.

The PR mentions the \A anchor. But the docs describe the A modifier. Changing A to \A on the docs side would at least make it not wrong (see the last two examples), but since the text is about avoiding substr(), it should still describe the A modifier.

So I think the original text from before that change is the best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants