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.

@lacatoire
lacatoire merged commit f88f34a into php:master Sep 27, 2026
3 checks passed
KentarouTakeda added a commit to php/doc-ja that referenced this pull request Oct 4, 2026
php/doc-en@aa5a948a11 (php/doc-en#5652)、php/doc-en@cdbe6f9826 (php/doc-en#5272)、
php/doc-en@f88f34ad2f (php/doc-en#5887) に追従します。

PREG_UNMATCHED_AS_NULL の説明に、末尾のマッチしなかったサブパターンの扱い
(フラグなしでは結果に含まれず、フラグありでは &null; として含まれる) を追加し、
その例と出力、7.4.0 の変更履歴を加えました。

offset の注記の文言は php/doc-en@aa5a948a11 で変更され、php/doc-en@f88f34ad2f で
元に戻されているため、該当文はタグの変更のみです。

## 対象ファイル (1件)

- reference/pcre/functions/preg-match.xml
@KentarouTakeda
KentarouTakeda deleted the fix-preg-match-offset-a-modifier branch October 4, 2026 05:38
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