Matheus Prado
Matheus Prado

Reputation: 85

How to replace all new lines in HTML content but preserve new lines inside textarea?

I have a PHP system that stores all the buffered output and before I dump it, I remove all new lines and spaces in whitespace. This works out right.

$content = Buffer::getKill();
$content = preg_replace(
[
    '/[\n\r]|/',
    '# {2,}#',
    '#> <#'
],
[
    '',
    ' ',
    '><'
],
$content
);

the reponsavel pattern that I tried to change was the following '/[\n\r]|/' but I did not succeed.

The problem is that when I have a textarea within content, new textarea lines are lost as well. I've tried to do the regular expression in several ways but I can not get results, because I'm unexpected with regular expressions.

Below is an example code and its output.

<html>
    <body>
        <div>
            <textarea>
                The
                Content
                Here 
            </textarea>
        </div> 
    </body>
</html>

Output

<html><body><div><textarea>TheContentHere</textarea></div></body></html>

Expected

<html><body><div><textarea>
    The
    Content
    Here
</textarea></div></body></html>

Upvotes: 0

Views: 143

Answers (2)

Brandon Nelson
Brandon Nelson

Reputation: 103

(?!<textarea[^>]*?>)(\n)(?![^<]*?<\/textarea>)

this will match any newlines not between text area tags.

Upvotes: 0

delboy1978uk
delboy1978uk

Reputation: 12365

Here's a regex thats looks for a >, followed by a newline (either \n or \r\n), followed by any number of spaces and a <:

$html = preg_replace('#>[\n\r\n]?\s+<#', '><', $html);

Which outputs :

<html><body><div><textarea> 
     The 
     Content 
     Here 
     </textarea></div></body></html>

Which aint perfect, but closer.

Upvotes: 1

Related Questions