Remove All the Line Breaks from the HTML Source

Remove all the line breaks from the html source

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);

How to remove all line breaks OUTSIDE of html tags

Something like this will suffice ?

<?php
$html=<<<SOMECONT
<p>some text</p>

<p>some other random
text with \n at fixed width
and thats great</p>
SOMECONT;

$narr=array_filter(explode(PHP_EOL,$html),'strlen');
echo implode('',$narr);

OUTPUT :

<p>some text</p><p>some other randomtext with 
at fixed widthand thats great</p>

EDIT : ALTERNATIVE

Might be more "dirty" but works. Afterall, removing all \n between htmltags can sometimes be as simple as removing empty lines from an exploded string of the original file.

  $split = explode(PHP_EOL,$data);
$data= "";
for($i = 0; $i < count($split); $i++){
$line = $split[$i];
else if(strlen($line) > 0) $data .= $split[$i]."\n"; // filter
}

How to remove all line breaks from a string

Line breaks (better: newlines) can be one of Carriage Return (CR, \r, on older Macs), Line Feed (LF, \n, on Unices incl. Linux) or CR followed by LF (\r\n, on WinDOS). (Contrary to another answer, this has nothing to do with character encoding.)

Therefore, the most efficient RegExp literal to match all variants is

/\r?\n|\r/

If you want to match all newlines in a string, use a global match,

/\r?\n|\r/g

respectively. Then proceed with the replace method as suggested in several other answers. (Probably you do not want to remove the newlines, but replace them with other whitespace, for example the space character, so that words remain intact.)

Remove line break HTML

You've got a few options:

p {
display: inline-block;
}

JS Fiddle demo.

Or:

p {
display: inline;
}

JS Fiddle demo.

If it's the white-space between the bottom of the first p and the top of the second p that you want to remove:

p {
/* top right bottom left */
margin: 0 0 0 0;
}

JS Fiddle demo.

How to remove line breaks (no characters!) from the string?

You should be able to replace it with a preg that removes all newlines and carriage returns. The code is:

preg_replace( "/\r|\n/", "", $yourString );

Even though the \n characters are not appearing, if you are getting carriage returns there is an invisible character there. The preg replace should grab and fix those.

How to remove line breaks from a string, but only between certain tags?

You can use two regex one to replace new line inside tags and another to remove between tags

  1. <[^>]+>[\s\S]+?<\/[^>]+> --> to remove new line inside tags

Sample Image


  1. (<\/[^>]+>)\n+(?=<[^>]+>) --> to remove new line between tags

Sample Image

let str = `<h1>Headline

One Do not touch</h1>

<p>

This is

a test string, please

put me on one line.

</p>

<p>

some text

</p>

`

let output = str.replace(/<[^>]+>[\s\S]+?<\/[^>]+>/g, m => m.replace(/\n+/g, ''))

let final = output.replace(/(<\/[^>]+>)\n+(?=<[^>]+>)/g,'$1\n')

console.log(final)

HTML/CSS - Remove spaces from line breaks in code for LI

You can float them (either left or right), or you can comment-out the spaces:

<ul>
<li>...</li><!--
--><li>...</li>
</ul>

Or simply leave the tags open 'til the next line.

<ul>
<li>...</li
><li>...</li
><li>...</li>
</ul>


Related Topics



Leave a reply



Submit