Wednesday, 16 October 2019

Converting between Line Breaks and HTML <br>


In different operating systems, different characters are used to define the end of a line in text files:

  • Windows: \r\n
  • Apple(Mac): \r
  • Linux: \n

However, in HTML, <br> tag is used to define a line break. If want to convert the text files in the current most popular operating systems, i.e. Windows, Apple and Linux, for HTML display, the special characters such as \r and \n should be replaced with the HTML <br> tag. The function str_replace() in PHP can be used to implement this task:

function toBR($str) {
    return str_replace(array("\r\n", "\n", "\r"), '<br>', $str);

$str = "Hello\r\ngood morning";
$str = toBR($str);
echo $str;

In line 2, the function str_replace() takes an array to find all occurrences of line break characters, i.e. \r\n, \r and \n, and replaces them with the HTML <br> tag. Since we need to find the line break' special characters in the string, double quotes (") should be used in the array definition in line 2. Otherwise, if single quote (') is used, they will not be considered as any special meaning.

On the contrary, if want to store the HTML text as one of the file formats such as Windows, Apple and Linux, the <br> tag should be replace as the specific line break characters. Regarding the input operating system option $os, the function toLineBreak() defined below replaces the <br> tag to be the relevant line break characters.

function toLineBreak($str, $os) {
    $pattern =  '/<\/?br\s*\/?>/i';
    switch ($os) {
        case 'WIN':
            return preg_replace($pattern, "\r\n", $str);
        case 'MAC':
            return preg_replace($pattern, "\r", $str);
        case 'LINUX':
            return preg_replace($pattern, "\n", $str);

$str = "Hello<br>good morning";
$str = toLineBreak($str, 'WIN');

In line 2, we define the search pattern $pattern for searching the HTML <br> tags by using the regular expression. According to the input operating system option $os, we call the PHP function preg_replace() to perform a regular expression search and replacement.