Logo Questions Linux Laravel Mysql Ubuntu Git Menu

php / html: replace html closing tags with newlines

I am crawling the web for html, and when I use php strip_tags it smushes the entire html into one line removing all structure.

I would like to preserve structure, by replacing closing h, p and br tags with newlines.

Would a preg replace be the best solution for this?

Once I replaced all closing tags I would run a strip tags but this way I would have a basic structure.

like image 735
giorgio79 Avatar asked Jan 18 '23 01:01


1 Answers

$str = 'some html';
$tags = array('</p>','<br />','<br>','<hr />','<hr>','</h1>','</h2>','</h3>','</h4>','</h5>','</h6>');
$str = str_replace($tags,"\n",$str);

// then strip tags
like image 188
Alex Coplan Avatar answered Jan 28 '23 00:01

Alex Coplan