Skip to content
PHP Logo

What is the best method to remove line breaks from formatted text in PHP?

When dealing with formatted text in PHP, line breaks can sometimes interfere with the desired output. To remove line breaks from formatted text, you can use the PHP function `str_replace()` to replace line breaks with an empty string. This will effectively remove all line breaks from the text, allowing you to display it without any unwanted line breaks.

php
<?php
$formatted_text = "This is some text with
line breaks that
need to be removed.";

$clean_text = str_replace(array("\r", "\n"), '', $formatted_text);

echo $clean_text;
?>