W3docs

hebrev()

The hebrev() function is used to convert Hebrew text to visual text for display on a web page. The syntax of the hebrev() function is as follows:

The PHP hebrev() function converts logical Hebrew text (stored in the order it is typed, right to left) into visual text (laid out left to right so a non-RTL display renders it correctly). It does this by reversing the run of Hebrew characters in the string while leaving punctuation and spacing readable.

This page covers what hebrev() did, its syntax and parameters, runnable examples, why it was removed, and what to use instead today.

Warning

hebrev() was deprecated in PHP 7.2 and completely removed in PHP 8.0. Calling it on PHP 8+ throws an Error: Call to undefined function hebrev(). This page is for understanding and maintaining legacy code only. For new code, let the browser handle direction with HTML dir="rtl" or CSS direction: rtl — see Why it was removed and what to use instead.

Syntax

hebrev(string $string, int $max_chars_per_line = 0): string
ParameterRequiredDescription
$stringYesThe Hebrew (logical) text to convert.
$max_chars_per_lineNoMaximum characters per line for word wrapping. When 0 (the default), no extra wrapping is applied.

The function returns the converted visual-order string. Non-Hebrew characters (ASCII letters, digits, punctuation) are left in place.

Basic example

Here is the simplest use of hebrev() — convert a logical Hebrew string to visual order:

php— editable, runs on the server

The Hebrew run is reversed so it reads correctly in a left-to-right environment. The output is:

תירבע רבדמ ינא ,ןכ

Wrapping lines with $max_chars_per_line

The second parameter inserts line breaks so that no line exceeds the given width. It only takes effect when a Hebrew run is longer than the limit — short strings like the one below fit on a single line, so the output is identical to the basic example. With 40 here, the text is well under the limit and is not wrapped:

php— editable, runs on the server

The output:

תירבע רבדמ ינא ,ןכ

To see wrapping actually trigger, you would need a Hebrew run longer than $max_chars_per_line; only then are line breaks inserted.

Why it was removed and what to use instead

hebrev() predates Unicode bidirectional rendering in browsers. The "visual order" trick was needed when terminals and old browsers could not reorder right-to-left text themselves. Today the rendering layer handles this, so reversing characters in PHP is both unnecessary and lossy (it bakes layout into your data).

For new code, keep text in its natural logical order and set the direction in markup:

<p dir="rtl">כן, אני מדבר עברית</p>

Or with CSS:

.hebrew { direction: rtl; }

The browser then reorders and wraps the text correctly, and the underlying string stays clean for search, copy-paste, and storage.

The companion hebrevc() function did the same conversion while also turning newlines into <br> tags; it was removed in PHP 8.0 as well — modern CSS wraps lines automatically.

  • hebrevc() — same conversion, with newlines converted to <br> (also removed in PHP 8.0).
  • strrev() — reverses every character in a string (not Hebrew-aware).
  • wordwrap() — the standard, still-supported way to wrap text at a given line width.
  • nl2br() — insert HTML line breaks before newlines in a string.

Practice

Practice
What does the hebrev() function in PHP do?
What does the hebrev() function in PHP do?
Was this page helpful?