Skip to content

Fix GH-1132: Document mb_strlen invalid encoding behavior change in P… - #5418

Merged
lacatoire merged 1 commit into
php:masterfrom
GabrielCordeiroBarrosoTeles:fix-gh-1132-mb-strlen-invalid-encoding-changelog
Aug 27, 2026
Merged

lacatoire merged 1 commit into
php:masterfrom
GabrielCordeiroBarrosoTeles:fix-gh-1132-mb-strlen-invalid-encoding-changelog

Conversation

@GabrielCordeiroBarrosoTeles

Copy link
Copy Markdown
Contributor

Fixes #1132

Documents that passing an invalid encoding to mb_strlen() had undefined behavior before PHP 8.0 (could return false) and that as of PHP 8.0 a ValueError is thrown instead, as suggested in the issue discussion.

Comment thread reference/mbstring/functions/mb-strlen.xml Outdated
@lacatoire

lacatoire commented Mar 24, 2026 •

Copy link
Copy Markdown
Member

Hi, i checked in php-src and the ValueError for invalid encodings comes from php_mb_get_encoding() (commit [21227dc1f19](php/php-src@21227dc1f19e
a9007ecfc6929261f16900359c4d), which is shared across many most functions, not just mb_strlen.

Currently only mb_convert_encoding and mb_encoding_aliases document this 8.0.0 change. Would it make sense to extend this PR to cover the other functions as well?

@kamil-tekiela

Copy link
Copy Markdown
Member

@lacatoire is right. This change was done to multiple functions and such such should be documented consistently. php/php-src@90eeca2#diff-28e30bf65ac518b331e0315af123e060f35e9053d7c05b68b5bcbcd1be080ba4

Since the change was already documented in one place, all you need to do is take it out and put it into a new snippet (language-snippets.ent) and then use that snippet for all the changed functions. Both the Errors/Exceptions and Changelog sections need to be adjusted.

…nippets

- Add mbstring.errors.encoding-invalid and mbstring.changelog.encoding-invalid
  entities to language-snippets.ent for consistent documentation
- Use these entities in mb_strlen, mb_internal_encoding, and mb_encoding_aliases,
  replacing per-function custom prose and fixing classname -> exceptionname
- Document that passing an invalid encoding to mb_strlen now throws a ValueError
  as of PHP 8.0.0; previously an E_WARNING was emitted and false was returned

Fixes phpGH-1132
@lacatoire
lacatoire force-pushed the fix-gh-1132-mb-strlen-invalid-encoding-changelog branch from da882ef to 3713d65 Compare August 27, 2026 12:51
@lacatoire
lacatoire merged commit 8465ca6 into php:master Aug 27, 2026
2 checks passed
KentarouTakeda added a commit to php/doc-ja that referenced this pull request Sep 22, 2026
* mb_strpos が返すのがバイト位置ではなく文字位置であることに追従

原文の php/doc-en@97f8edc (php/doc-en#5853) に追従する。

- 戻り値が、バイト位置ではなく 0 から数えた文字位置であることの明確化
- offset が文字単位であることの明記
- 文字オフセットとバイトオフセットが食い違う例の追加
- seealso への mb_strrpos と mb_stripos の追加

* mb_scrub の例を原文に追従

原文の php/doc-en@eb10503 (php/doc-en#5811) に追従する。

- bin2hex と var_dump でバイト列そのものを確認する例の追加 (端末やブラウザ、
  フォントが独自の置換文字で表示するため、見た目だけでは判断できない)
- 結果が mbstring.substitute_character に依存することを示す例
- 不正なバイト列を置き換えると u 修飾子つきの PCRE パターンがマッチするようになる例
- seealso の追加

* mb_decode_numericentity と mb_encode_numericentity の ValueError の条件とコード例に追従

原文の php/doc-en@3505aae (php/doc-en#4852) と
php/doc-en@0bb8d1a (php/doc-en#4708) に追従する。

- ValueError がスローされる条件が「map が整数のリストでない場合」から
  「map に int、float、bool、null、数値形式の文字列のいずれでもない値が
  含まれている場合」に明確化されたこと
- changelog 8.4.0 の条件が「map に暗黙のうちに int に変換できない値が
  含まれている場合」に変わったこと
- コード例の整形 (array( の記法、インデント、閉じ括弧の位置、末尾の ?>)

* 不正なエンコーディングを渡した場合の説明を共有エンティティに追従

原文の php/doc-en@8465ca6 (php/doc-en#5418) に追従する。

- mb_strlen / mb_internal_encoding / mb_encoding_aliases の各ページが
  独自に書いていた説明を、共有エンティティの参照に置き換え
- mb_strlen に、不正なエンコーディングを渡すと PHP 8.0.0 以降は
  ValueError をスローすること (これより前は E_WARNING が発生し、
  false を返していた) を追記
- ValueError のマークアップの誤りを classname から exceptionname に修正

* mb_http_input が ValueError をスローするようになったバージョンの誤りを原文に合わせて修正

原文の php/doc-en@2fab267 (php/doc-en#5252) に追従する。

- type が不正な場合に ValueError をスローするようになったのは
  8.4.0 ではなく 8.0.0 であることの修正

* mb_encode_numericentity のコード例のコメントを翻訳

mb_decode_numericentity の同じコード例では訳されているコメントが、
mb_encode_numericentity では英語のまま残っていた。

あわせて、同じ内容のコメントが 2 ページで食い違わないよう、
mb_decode_numericentity 側の既訳も同一の文面に揃える。offsetN は
値に「追加」するのではなく加算するもので、そこも合わせて直す。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Providing an incorrect encoding to mb_strlen returns false before PHP 8.0

4 participants