PHP hebrev() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, hebrev() rearranges logical Hebrew text into visual order for display in older contexts.

The string is required. An optional max line length can insert newlines while converting.

Syntax

Call hebrev on the Hebrew string. Start with this line.

PHP
$visual = hebrev($logical);
CallWhat it does
hebrev($logical)Converts logical Hebrew to visual order.
hebrev($logical, 40)Also wraps near 40 characters.

Modern apps usually prefer proper Unicode bidirectional rendering instead.

Library declarations
PHP
hebrev(string $string, int $max_chars_per_line = 0): string

Parameters

ParameterDescription
stringRequired. Logical Hebrew text.
max_chars_per_lineOptional. Wrap width. Default 0 means no wrapping.

Return Value

Return ValueDescription
stringHebrew text converted to visual order.

PHP Examples

Example 1 — Wrap with max_chars_per_line

When the second argument is greater than 0, PHP wraps the visual text and tries not to split words.

PHP
<?php
echo hebrev("This is a long line of text", 14);

How It Works

  1. Input. One long sentence as a single string.
  2. Width. 14 asks for wrapping near 14 characters.
  3. Flow. Lines are arranged for visual Hebrew-style reading.
  4. No wrap. Omit the second argument (or pass 0) for a single line.

Example 2 — Convert a Hebrew Word

Pass logical Hebrew; get a visual string for legacy plain-text output. Save the file as UTF-8.

PHP
<?php
// "shalom" in Hebrew letters
$logical = "שלום";
echo hebrev($logical);

How It Works

  1. Logical input. The string holds Hebrew in typing/storage order.
  2. Convert. hebrev() rearranges for visual (non-BiDi) display.
  3. Terminal note. Your console may still render RTL visually—compare in a strict LTR plain-text viewer.
  4. Encoding. Historically aimed at single-byte Hebrew; for the web, prefer dir="rtl".

Example 3 — Prefer HTML RTL for Web Pages

On modern sites, let the browser handle BiDi. Escape output with htmlspecialchars().

PHP
<?php
$text = "שלום";
// Better for HTML than hebrev():
echo '<p lang="he" dir="rtl">'
   . htmlspecialchars($text, ENT_QUOTES, "UTF-8")
   . "</p>";

How It Works

  1. Keep logical order. Do not rearrange with hebrev() for HTML.
  2. Mark direction. dir="rtl" and lang="he" tell the browser to render RTL.
  3. Escape. htmlspecialchars() keeps the page safe from HTML injection.
  4. When hebrev fits. Use it for plain-text / legacy channels without BiDi.

Frequently Asked Questions

hebrev() converts logical Hebrew text into visual Hebrew text. Logical order is how Hebrew is stored when typed; visual order rearranges characters for systems that do not support bidirectional (BiDi) rendering—such as plain-text email or old terminals.
string hebrev(string $string, int $max_chars_per_line = 0). The first argument is the input string. The optional second argument sets a maximum line width; when greater than 0, PHP wraps the visual text and tries not to break words.
Logical order follows typing and Unicode storage (used by modern browsers with dir="rtl"). Visual order rearranges characters for left-to-right-only displays. hebrev() produces visual order for legacy plain-text contexts.
Usually no. Prefer HTML lang="he" and dir="rtl" (or CSS direction: rtl) so the browser handles BiDi. Reserve hebrev() for plain-text output where BiDi is unavailable.
hebrev() returns one visual string. hebrevc() also converts newlines to <br>\ for HTML. hebrevc() was removed in PHP 8—use nl2br(hebrev($text)) if you still need that behavior.

Next: hebrevc()

Learn the hebrev + <br> variant and what to use on PHP 8.

hebrevc tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful