Signature
hebrev(string $string, int $max_chars_per_line = 0): stringPurpose
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 default0means 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().
$logical = "\xE0\xE1\xE2";
echo bin2hex(hebrev($logical)), "\n";e2e1e0
The three-letter run comes back in reverse byte order: e2e1e0.
Example 2: non-Hebrew text passes through
echo json_encode([hebrev("Hello 123"), hebrev("")]), "\n";["Hello 123",""]
Latin letters, digits and an empty string are left as they are.
Example 3: UTF-8 input is not converted
$text = "\u{05D0}\u{05D1}";
$result = hebrev($text);
echo json_encode($result === $text), "\n";
echo bin2hex($result), "\n";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_linecan 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
- 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.
- Applying it twice. The result is already in visual order. Running it again on the same data gives wrong text.
- Using it for modern web pages. HTML and browsers already support right-to-left text through the
dirattribute and the Unicode bidirectional algorithm. Storing visually ordered text makes searching and editing harder. - Ignoring the line breaks that a positive
$max_chars_per_linemay produce. - Printing raw output into UTF-8 pages. The bytes may display as garbage or be rejected.
Related functions
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
$logical = "\xE0\xE1\xE2";
echo bin2hex(hebrev($logical)), "\n";
e2e1e0
echo json_encode([hebrev("Hello 123"), hebrev("")]), "\n";
["Hello 123",""]
$text = "\u{05D0}\u{05D1}";
$result = hebrev($text);
echo json_encode($result === $text), "\n";
echo bin2hex($result), "\n";
true d790d791