Strings7 min readExamples run on PHP 8.5.10

hebrev()

Learn how PHP hebrev() converts logical Hebrew text to visual order, which encoding it expects, and where UTF-8 input goes wrong.

Signature

hebrev(string $string, int $max_chars_per_line = 0): string

Purpose

Hebrew is written right to left. Text is normally stored in logical order: the first character in the string is the first one a reader reads. Some old display systems, however, draw text strictly left to right and need the characters already arranged in visual order. hebrev() converts logical Hebrew text into that visual order, so a left-to-right display shows it correctly.

The manual also notes that the function tries to avoid breaking words when it wraps text. It is a niche, legacy-oriented helper. Modern browsers and UI toolkits handle right-to-left text themselves, so you rarely need it.

Signature

Parameters and defaults

  • $string: the Hebrew input in logical order.
  • $max_chars_per_line: an optional maximum number of characters per output line. The default 0 means no line limit is requested. With a positive value the function may insert line breaks, trying not to split words.

Return value and failure cases

The function returns the visual string. It does not return false and does not signal failure for unusual input. Text it does not recognize as Hebrew is simply passed through in its original order. An empty string gives an empty string.

Encoding matters

hebrev() works on single bytes. It recognizes Hebrew letters in a single-byte Hebrew encoding such as ISO-8859-8, where the letters occupy bytes 0xE0 to 0xFA. In UTF-8, each Hebrew letter takes two bytes, and those bytes fall outside that range. A UTF-8 Hebrew string is therefore not reordered.

Example 1: reversing a Hebrew run

The string below contains three ISO-8859-8 letters (alef, bet, gimel) as raw bytes. Printing them raw would produce invalid UTF-8 on a web page, so the example prints the bytes as hexadecimal with bin2hex().

Example
<?php
$logical = "\xE0\xE1\xE2";
echo bin2hex(hebrev($logical)), "\n";
Output
e2e1e0

The three-letter run comes back in reverse byte order: e2e1e0.

Example 2: non-Hebrew text passes through

Example
<?php
echo json_encode([hebrev("Hello 123"), hebrev("")]), "\n";
Output
["Hello 123",""]

Latin letters, digits and an empty string are left as they are.

Example 3: UTF-8 input is not converted

Example
<?php
$text = "\u{05D0}\u{05D1}";
$result = hebrev($text);
echo json_encode($result === $text), "\n";
echo bin2hex($result), "\n";
Output
true
d790d791

The two UTF-8 encoded letters (d790 and d791) come back unchanged, because their bytes are not in the single-byte Hebrew range.

Edge cases

  • Mixed text: Hebrew runs are reordered while other text keeps its place. Test mixed strings with your own data and encoding before relying on the exact layout.
  • Line limits: a positive $max_chars_per_line can add newline characters to the result. Treat the return value as possibly multi-line, and keep your display code ready for that.
  • Punctuation and spaces: these characters can belong to a surrounding Hebrew run, so they may move with it.
  • Single-byte output: the result can contain bytes that are invalid UTF-8. Avoid passing it to functions that require valid UTF-8, such as json_encode(), without converting it first.

Common mistakes

  1. Passing UTF-8 and expecting reversal. As Example 3 shows, nothing happens. Convert to a single-byte Hebrew encoding first if you truly need visual ordering.
  2. Applying it twice. The result is already in visual order. Running it again on the same data gives wrong text.
  3. Using it for modern web pages. HTML and browsers already support right-to-left text through the dir attribute and the Unicode bidirectional algorithm. Storing visually ordered text makes searching and editing harder.
  4. Ignoring the line breaks that a positive $max_chars_per_line may produce.
  5. Printing raw output into UTF-8 pages. The bytes may display as garbage or be rejected.

The manual lists hebrevc(), which converted logical Hebrew text and also converted newlines. For general string work, look at strrev() (plain byte reversal, not Hebrew-aware), wordwrap() (line wrapping), mb_convert_encoding() (changing encodings) and bin2hex() (inspecting raw bytes).

Recap

hebrev() turns logical Hebrew into visual order for left-to-right output and optionally limits line length. It expects single-byte Hebrew text, passes other text through, and has no failure value. For UTF-8 data it does nothing useful, and modern right-to-left support usually makes it unnecessary.

Tested examples

Reversing a run of ISO-8859-8 Hebrew letters
<?php
$logical = "\xE0\xE1\xE2";
echo bin2hex(hebrev($logical)), "\n";
Output
e2e1e0
Non-Hebrew text passes through unchanged
<?php
echo json_encode([hebrev("Hello 123"), hebrev("")]), "\n";
Output
["Hello 123",""]
UTF-8 Hebrew input is not converted
<?php
$text = "\u{05D0}\u{05D1}";
$result = hebrev($text);
echo json_encode($result === $text), "\n";
echo bin2hex($result), "\n";
Output
true
d790d791