You can use a combination of trim to remove leading and trailing whitespace, and preg_replace to replace all newlines and their surrounding spaces internal to the string with a single space:

$str = preg_replace('/\s*\R\s*/', ' ', trim($str));
echo $str;

Note in the above regex \R matches any newline (\r, \n) character.

Output:

This Like a Somthing awesome text with goods:Fb,Teleg,Top,Prods.fm,Ad-...

Demo on 3v4l.org

Answer from Nick on Stack Overflow
🌐
GitHub
gist.github.com › abdullahbutt › 872e982049048d980ac89e7d1107c614
Remove Whitespace and line breaks from String using PHP · GitHub
Remove Whitespace and line breaks from String using PHP · Raw · Remove Whitespace and line breaks from String using PHP · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
Sentry
sentry.io › sentry answers › php › how do i strip all spaces out of a string in php?
PHP Remove All Spaces from a String with str_replace | Sentry
July 31, 2026 - Use str_replace() to remove spaces or preg_replace() with a regex pattern to strip all whitespace including tabs and line breaks from a PHP string
🌐
PHP
php.net › manual › en › function.trim.php
PHP: trim - Manual
Because trim() trims characters from the beginning and end of a string, it may be confusing when characters are (or are not) removed from the middle. trim('abc', 'bad') removes both 'a' and 'b' because it trims 'a' thus moving 'b' to the beginning to also be trimmed. So, this is why it "works" whereas trim('abc', 'b') seemingly does not. ltrim() - Strip whitespace (or other characters) from the beginning of a string
People also ask

Can trim() remove newline characters?
Yes. It removes \n, \r, and other whitespace characters by default.
🌐
flatcoding.com
flatcoding.com › home › php trim(): how to remove whitespace from string
PHP trim(): How to remove Whitespace from String - FlatCoding
Does trim() affect UTF-8 or multibyte strings?
It does not break them. But it works at the byte level, not the character level. Use mb_trim() from custom libraries for complex scripts.
🌐
flatcoding.com
flatcoding.com › home › php trim(): how to remove whitespace from string
PHP trim(): How to remove Whitespace from String - FlatCoding
🌐
W3Schools
w3schools.com › php › func_string_trim.asp
PHP trim() Function
zip_close() zip_entry_close() zip_entry_compressedsize() zip_entry_compressionmethod() zip_entry_filesize() zip_entry_name() zip_entry_open() zip_entry_read() zip_open() zip_read() PHP Timezones ... The trim() function removes whitespace and ...
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-remove-line-breaks-from-the-string-in-php
How to remove line breaks from the string in PHP? - GeeksforGeeks
July 11, 2025 - <?php // Declare a variable and ... // Display the string echo $text; echo "\n"; // Use str_replace() function to // remove <br> tag $text = str_replace("<br>", "", $text); // Display the new string echo $text; ?>...
🌐
Uptimia
uptimia.com › home › questions › how to remove all spaces from a string in php?
How To Remove All Spaces From A String In PHP?
October 16, 2024 - The preg_replace() method is better than str_replace() for handling various types of whitespace, making it useful when you need to remove all kinds of spaces and line breaks from your string.
🌐
Texthandler
texthandler.com › info › remove-line-breaks-php
Remove line breaks with PHP
This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.. ... //Removes all 3 types of line breaks $string = str_replace("\r", "", $string); $string = str_replace("\n", "", $string);
Find elsewhere
🌐
FlatCoding
flatcoding.com › home › php trim(): how to remove whitespace from string
PHP trim(): How to remove Whitespace from String - FlatCoding
August 19, 2025 - They include tabs, breaks, and nulls. They often sneak into strings during user input or file reads. To specify which characters to remove you can give trim() a second argument. That list tells it what to remove. $text = "///Flatcoding php tutorials///"; echo trim($text, "/"); ... It removes the space on both sides of the ‘apple’ text. ... All control characters are gone.
🌐
Techie Delight
techiedelight.com › home › java › strip all spaces from a string in php
Strip all spaces from a string in PHP | Techie Delight
July 7, 2026 - To strip all whitespace characters from the string, you can use the regular expression \s+ which matches one or more whitespace characters. In PHP, you can use the preg_replace() function to perform search and replace using a regex.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-strip-all-spaces-out-of-a-string-in-php.php
How to Strip All Spaces Out of a String in PHP
<?php $str = 'This is a simple piece of text.'; $new_str = str_replace(' ', '', $str); echo $new_str; // Outputs: Thisisasimplepieceoftext. ?> The above example will however only remove spaces. If you want to remove all whitespaces including tabs, newlines, etc.
🌐
Infoheap
infoheap.com › home › tutorials › php
PHP remove trailing whitespaces and newline - InfoHeap
February 2, 2016 - PHP has inbuilt function rtrim to remove specific or all trailing whitespaces from end of php string. preg_replace can also be used for this purpose. But using rtrim is more elegant.
🌐
Scaler
scaler.com › home › topics › php trim() function
PHP trim() function - Scaler Topics
March 31, 2024 - The trim() function in PHP is a powerful tool used to remove whitespace or other specified characters from the beginning and end of a string. It helps to sanitize and clean user input by eliminating unnecessary spaces, tabs, line breaks, and other characters that might affect data validation ...
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-remove-all-white-spaces-from-a-string-in-php
How to remove all white spaces from a string in PHP ? - GeeksforGeeks
July 11, 2024 - This approach transforms the string into a contiguous sequence of characters, useful for processing data where whitespace is irrelevant. Example : PHP code splits the string "Hello World" into an array at spaces using `explode()`, then removes spaces by joining array elements with `implode()`, outputting "HelloWorld".
🌐
Stack Overflow
stackoverflow.com › questions › 21306693 › remove-whitespaces-or-line-break-within-a-string
php - Remove whitespaces or line break within a String - Stack Overflow
UPDATE (2014-01-24): How about this one from viewSource also (im not sure if its a whitespaces or newline) instead of <br> ? ... There's no <br /> in your input string.
🌐
Bomberbot
bomberbot.com › php › removing-line-breaks-from-strings-in-php-an-in-depth-exploration
Removing Line Breaks from Strings in PHP: An In-Depth Exploration - Bomberbot
August 21, 2025 - The \R pattern is particularly useful as it matches any Unicode newline sequence, making it more versatile than simple string replacement. This method not only removes line breaks but also replaces them with a space, which can be preferable ...