The hebrev() function converts logical Hebrew (storage/typing order) into visual Hebrew (display order for left-to-right-only systems). It is a legacy helper for plain text—modern web pages should use HTML/CSS BiDi instead.
01
Hebrew RTL
Right-to-left script.
02
Logical → Visual
Reorder for display.
03
Line Wrapping
max_chars_per_line.
04
Plain Text
Email, logs, terminals.
05
Not for HTML
Use dir="rtl".
06
hebrevc()
Newline conversion.
Fundamentals
Definition and Usage
In PHP, hebrev() transforms Hebrew strings from logical order into visual order. When you type Hebrew, characters are stored in logical sequence. Browsers and modern Unicode-aware tools render that correctly with BiDi rules. Older plain-text channels (some email clients, terminals, log files) do not—so PHP can pre-arrange the characters with hebrev().
💡
Beginner Tip
If you are building a normal HTML page, skip hebrev() and wrap Hebrew content in an element with dir="rtl" and lang="he". Use hebrev() only when the output target cannot handle BiDi.
Foundation
📝 Syntax
The function accepts Hebrew text and an optional line-width limit:
PHP
string hebrev(
string $string,
int $max_chars_per_line = 0
)
Parameters
$string — Hebrew input in logical order.
$max_chars_per_line — optional maximum characters per line. 0 (default) means no wrapping; a positive value wraps the visual output and tries to avoid breaking words.
Return Value
Returns a string containing the visual representation of the input Hebrew text.
Cheat Sheet
⚡ Quick Reference
Basic convert
hebrev($hebrew)
Logical to visual
Wrap lines
hebrev($hebrew, 40)
Max 40 chars/line
Compare
$a === hebrev($a)
Usually false
Modern HTML
dir="rtl" lang="he"
Preferred for web
Hands-On
Examples Gallery
These examples use UTF-8 Hebrew strings in PHP 7+. Each shows a pattern beginners encounter when working with hebrev().
📚 Getting Started
Convert a simple Hebrew greeting from logical to visual order.
Example 1 — Basic Hebrew Conversion
Convert the classic greeting “Hello, world!” in Hebrew and print both forms.
The visual string rearranges characters so the greeting reads correctly on a left-to-right-only display. Punctuation and Hebrew segments are repositioned according to PHP’s Hebrew conversion rules.
Example 2 — Confirm Logical and Visual Differ
A quick check that hebrev() actually changed the string—useful when debugging encoding pipelines.
Same string? no
Logical length: 8
Visual length: 8
How It Works
Byte length stays the same (UTF-8 Hebrew letters), but character order changes. The strings are not identical even though they represent the same word.
📈 Practical Patterns
Line wrapping, mixed-language text, and modern alternatives.
Example 3 — Line Wrapping with max_chars_per_line
Wrap a longer Hebrew sentence for plain-text email or fixed-width output.
PHP
<?php
$sentence = "זהו משפט ארוך בעברית שמדגים שבירת שורות בטקסט ויזואלי";
$wrapped = hebrev($sentence, 20);
$lines = explode("\n", $wrapped);
echo "Line count: " . count($lines) . "\n";
echo "First line chars: " . strlen($lines[0]) . "\n";
?>
📤 Output:
Line count: 3
First line chars: 20
How It Works
When $max_chars_per_line is greater than zero, PHP inserts newline characters to keep each line within the limit. It attempts to avoid splitting Hebrew words mid-word.
Example 4 — Mixed Hebrew and Latin Text
Real content often mixes English product names or numbers with Hebrew. hebrev() handles BiDi segments in the conversion.
Latin segments and numbers are repositioned relative to Hebrew runs so the full line reads naturally on an LTR-only display. Run the script locally to inspect the exact visual string for your PHP version.
Example 5 — When to Skip hebrev() (Modern HTML)
For web pages, let the browser handle direction instead of pre-converting text.
PHP
<?php
$hebrew = "שלום, עולם!";
// Plain-text log (no BiDi) — hebrev() helps:
file_put_contents('log.txt', hebrev($hebrew) . "\n");
// HTML page — use dir/lang, NOT hebrev():
$html = '<p dir="rtl" lang="he">' .
htmlspecialchars($hebrew, ENT_QUOTES, 'UTF-8') .
'</p>';
echo "Log uses hebrev(); HTML uses dir=rtl.\n";
?>
📤 Output:
Log uses hebrev(); HTML uses dir=rtl.
How It Works
Choose the tool for the output medium: hebrev() for plain text without BiDi support; HTML attributes and CSS for normal web rendering. Always pair HTML output with htmlspecialchars() for safety.
Applications
🚀 Common Use Cases
Plain-text email — format Hebrew body text for clients without BiDi support.
Terminal / CLI output — print Hebrew to consoles that only render left-to-right.
Log files — store human-readable Hebrew in fixed-width log viewers.
Legacy integrations — systems predating Unicode BiDi that expect visual-order strings.
Learning Hebrew encoding — understand logical vs visual order before using modern RTL CSS.
🧠 How hebrev() Works
1
You pass logical Hebrew
The input string is in logical (typing/storage) order—what you would store in a UTF-8 database field.
Input
2
PHP reorders characters
The function applies Hebrew visual-order rules, repositioning RTL runs and neighboring Latin or numeric segments.
Transform
3
Optional line wrap
If $max_chars_per_line is set, PHP inserts newlines while trying to keep Hebrew words intact.
Wrap
=
🌐
Visual string
Ready for plain-text channels that cannot render BiDi themselves.
Important
📝 Notes
hebrev() supports Hebrew only—not Arabic or other RTL languages.
For modern HTML, use dir="rtl" and lang="he" instead of pre-converting with hebrev().
Requires a PHP build with Hebrew conversion support (available in standard PHP builds).
Pair with hebrevc() when you also need newline-to-<br> conversion for HTML snippets.
Always use UTF-8 encoding for Hebrew source strings and file output.
Wrap Up
Conclusion
The hebrev() function bridges logical Hebrew storage and visual display for plain-text environments. It remains useful for logs, email, and legacy systems, but it is not the right tool for modern web pages.
Match your approach to the output medium: hebrev() for BiDi-less plain text, HTML/CSS direction for browsers, and hebrevc() when newline conversion is also required.
Set max_chars_per_line for fixed-width plain-text layouts
Test output in the actual target (email client, terminal, log viewer)
Consider hebrevc() when newlines must become <br>
❌ Don’t
Run hebrev() on HTML that already uses BiDi CSS
Expect it to work with Arabic or other RTL scripts
Double-convert text (hebrev twice corrupts display)
Skip htmlspecialchars() when embedding Hebrew in HTML
Assume every email client still needs visual-order conversion
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about hebrev()
Use these points whenever you handle Hebrew strings in PHP.
5
Core concepts
🌐01
Logical → Visual
Reorder for LTR display.
Purpose
📝02
Two Parameters
Text + optional width.
Syntax
📄03
Line Wrapping
max_chars_per_line.
Option
📧04
Plain Text
Email, logs, CLI.
Usage
🎨05
Use CSS for Web
dir="rtl" in HTML.
Modern web
❓ 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 so the text reads correctly on systems that do not support bidirectional (BiDi) text rendering—such as plain-text email or old terminals.
string hebrev(string $string, int $max_chars_per_line = 0). The first argument is the Hebrew 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 order (used by modern browsers with dir="rtl"). Visual order is a character rearrangement for left-to-right-only displays. hebrev() produces visual order for legacy plain-text contexts.
Usually no. For web pages, prefer HTML lang="he" and CSS direction: rtl (or dir="rtl") so the browser handles BiDi correctly. Reserve hebrev() for plain-text output where BiDi is unavailable.
hebrev() returns one visual string. hebrevc() does the same conversion and also converts newlines (\n) to <br>\n for HTML output. Use hebrevc() only when you need that newline conversion in HTML snippets.
No. hebrev() is designed for Hebrew only. For Arabic and other right-to-left scripts, use proper Unicode BiDi support in your environment rather than this function.