Skip to content

RIGHT: return all of the text when num_chars exceeds its length - #5044

Merged
oleibman merged 3 commits into
PHPOffice:masterfrom
kevin9327:fix-right-longer-than-text
Oct 18, 2026
Merged

oleibman merged 3 commits into
PHPOffice:masterfrom
kevin9327:fix-right-longer-than-text

Conversation

@kevin9327

Copy link
Copy Markdown
Contributor

This is:

  • a bugfix
  • a new feature
  • refactoring
  • additional unit tests

Checklist:

  • Changes are covered by unit tests
    • Changes are covered by existing unit tests
    • New unit tests have been added
  • Code style is respected
  • Commit message explains why the change is made (see https://github.com/erlang/otp/wiki/Writing-good-commit-messages)
  • CHANGELOG.md contains a short summary of the change and a link to the pull request if applicable
  • Documentation is updated as necessary

Why is this change needed?

RIGHT drops characters from the start of the result when num_chars is bigger than the text:

=RIGHT("abc",5)   -> "bc"   (Excel: "abc")
=RIGHT("abc",4)   -> "c"    (Excel: "abc")

The start offset mb_strlen($value) - $chars goes negative, and mb_substr then counts it from the end of the string. Excel's docs say "If num_chars is greater than the length of text, RIGHT returns all of text", and LibreOffice does the same. This PR clamps the offset at 0.

I added two cases to the RIGHT data provider: one ASCII and one multibyte, where the length is exactly one more than the text. Both fail without the fix. The TextData function tests pass, and php-cs-fixer, phpcs, and phpstan are clean on the changed file.

kevin9327 and others added 3 commits October 5, 2026 12:32
RIGHT("abc",5) returned "bc" because the start offset went negative and
mb_substr then counted it from the end of the string. Excel returns the
whole text when num_chars is greater than its length, so clamp the offset
at 0.
@oleibman
oleibman added this pull request to the merge queue Oct 18, 2026
Merged via the queue into PHPOffice:master with commit 43e1f6c Oct 18, 2026
14 checks passed
@oleibman

Copy link
Copy Markdown
Collaborator

Thank you for your contribution.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants