Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all the line breaks from the html source

Well I know obfuscation is a bad idea. But I want all of my html code to come in one long single line. All the html tags are generated through PHP, so I think its possible. I knew replacing \n\r from regular expression, but have no idea how to do this one. In case I am unclear here is an example

$output = '<p>               <div class="title">Hello</div>            </p>'; echo $output; 

To be view in the source viewer as <p><div class="title">Hello</div></p>

like image 521
mrN Avatar asked Mar 10 '11 10:03

mrN


People also ask

How do you remove a line break in HTML?

“how to remove line break in html” Code Answer's -- One way is to delete the used (if used <br> tags). --!> <! -- The other solution is to use a little css and set the html elements (display to inline).

How do you remove line breaks from a string?

Use the String. replace() method to remove all line breaks from a string, e.g. str. replace(/[\r\n]/gm, ''); . The replace() method will remove all line breaks from the string by replacing them with an empty string.

How do I remove a line break in a text file?

Open TextPad and the file you want to edit. Click Search and then Replace. In the Replace window, in the Find what section, type ^\n (caret, backslash 'n') and leave the Replace with section blank, unless you want to replace a blank line with other text.


1 Answers

Maybe this?

$output = str_replace(array("\r\n", "\r"), "\n", $output); $lines = explode("\n", $output); $new_lines = array();  foreach ($lines as $i => $line) {     if(!empty($line))         $new_lines[] = trim($line); } echo implode($new_lines); 
like image 185
seriousdev Avatar answered Oct 14 '22 17:10

seriousdev