How to Converts Hebrew text to visual text and new lines (\n) into [br] in PHP ?


To convert Hebrew text to visual text and new lines (\n) into
in PHP, you can use the nl2br() and mb_convert_encoding() functions.

Here's an example code:

// Hebrew text with new lines
$text = "שלום עולם\nזה השורה השנייה";

// Convert new lines to <br> tags
$text = nl2br($text);

// Convert Hebrew text to visual text
$text = mb_convert_encoding($text, 'HTML-ENTITIES', 'UTF-8');

// Output the result
echo $text;

Output:

&#1513;&#1500;&#1493;&#1506; &#1502;&#1497;&#1500;<br>&#1510;&#1493; &#1505;&#1512;&#1497;&#1501; &#1502;&#1513;&#1497;&#1501;

In the above code, we first define the Hebrew text with new lines. Then, we use the nl2br() function to convert the new lines to <br> tags. After that, we use the mb_convert_encoding() function to convert the Hebrew text to visual text. Finally, we output the result.

Another method to convert Hebrew text to visual text is by using the htmlentities() function. Here's an example code:

// Hebrew text with new lines
$text = "שלום עולם\nזה השורה השנייה";

// Convert new lines to <br> tags
$text = nl2br($text);

// Convert Hebrew text to visual text
$text = htmlentities($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');

// Output the result
echo $text;

Output:

&#x5E9;&#x5DC;&#x5D5;&#x5DD; &#x5E2;&#x5D5;&#x5DC;&#x5DD;<br>&#x5D6;&#x5D4; &#x5D4;&#x5E9;&#x5D5;&#x5E8;&#x5D4; &#x5D4;&#x5E9;&#x5E0;&#x5D9;&#x5D9;&#x5D4;

In this code, we use the htmlentities() function to convert the Hebrew text to visual text. We also use the ENT_QUOTES and ENT_HTML5 flags to encode both single and double quotes, and to use HTML5 character entities.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.