Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove white spaces between tags in HTML

I am using the following code to remove white spaces in html. I only want to remove white spaces in betweens tags. But below code replaces all white spaces

I.E remove all white spaces in between ">" and "<"

//read the entire string
$str=file_get_contents('sample.txt');

//replace all white spaces
$str=str_replace("\n", "",$str);
$str=str_replace("\t", "",$str);
$str=str_replace(" ", "",$str);

//write the entire string
file_put_contents('sample.txt', $str);
like image 842
Gijo Varghese Avatar asked Mar 19 '16 00:03

Gijo Varghese


People also ask

How do I get rid of extra white space in HTML?

In order to get rid of additional white space, there are 3 properties that can be used: Using the display property. Using vertical-align property. Using line-height property.


1 Answers

You need use a regular expresion.

Maybe you can use this:

$html = preg_replace('/\>\s+\</m', '><', $html);

Test here https://repl.it/

like image 148
Mauricio Florez Avatar answered Sep 21 '22 08:09

Mauricio Florez