html - PHP replacing break lines but still show new line

Solution:

Your regular expression is wrong. Try this:

/**
* Convert BR tags to nl
*
* @param string The string to convert
* @return string The converted string
*/
function br2nl($string)
{
    return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
}

Credits: This code snippet comes from the comments from the php site. https://www.php.net/manual/en/function.nl2br.php

Answer

Solution:

Strip tags might help you...

<?php

    $mytxt = '*e.g. Lorem ipsum<br><br>
    Another lorem ipsum<br><br>
    Third section...*';

    $final = strip_tags($mytxt);

If you were to echo this in your browser for example, it would appear that there are no new lines, but if you were to view the source of your page you would see that the newlines are infact still intact.

Further information: https://www.php.net/manual/en/function.strip-tags.php

Source