---
title: How to Remove All Line Breaks from Text Using Regex
date: 2012-02-18T10:45:27+00:00
modified: 2020-08-11T08:10:30+00:00
permalink: https://kaspars.net/blog/regex-remove-all-line-breaks-from-text
post_type: post
author:
  name: Kaspars
  avatar: https://reverse.kaspars.net/gravatar/avatar/92bfcd3a8c3a21a033a6484d32c25a40b113ec6891f674336081513d5c98ef76?s=96&d=mm&r=g
category:
  - Development
post_tag:
  - HTML
  - PHP
  - Snippet
---

# How to Remove All Line Breaks from Text Using Regex

If you are generating `<meta>` *description* tags automatically (e.g. by [including all headings of the document](https://kaspars.net/blog/regex-extract-headings-h1-h2-h3-from-html "Extract headings and headlines from HTML documents")), chances are that you’re extracting it from various sources of content that contain different HTML elements and line breaks in them.

Here is a simple regular expression to **remove all line breaks, carriage returns and tabs**, and replace them with an empty space.

```
$text = preg_replace( '/(\r\n)+|\r+|\n+|\t+/', ' ', $text )
```

It works by replacing all instances of Windows and unix line breaks and tabs with a blank space character.