[PHP] Remove line breaks from a string

Created on November 30, 2026

You can pass an array of line break sequences to str_replace() and replace each with an empty string. This example removes Windows-style \r\n, single carriage returns \r, and line feeds \n.

<?php

$text = "Hello\r\nWorld\rGood\nBye";
$result = str_replace(["\r\n", "\r", "\n"], '', $text);
echo $result, PHP_EOL;
HelloWorldGoodBye
PHP