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:
שלוע מיל<br>צו סרים משים
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:
שלום עולם<br>זה השורה השנייה
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.