How to clean up text with PHP – remove line breaks, paragraphs and tabs and replace with an empty space

For some text, like Meta description tags and excerpts, you may not want to have paragraph formatting, empty lines, breaks in the text. Especially when you are extracting this text automatically to generate the Meta description.

This is a simple regular expression to remove line breaks, paragraphs and tabs, and replace them with an empty space.

$text = preg_replace(“/rn+|r+|n+|t+/i”, ” “, $text);

How to extract H1 tags from a page to pull headings from posts and pages:

preg_match_all('|<h[^>]+>(.*)</h[^>]+>|iU', $html, $headings);

Leave a comment