0

I have a string:

$str = '
            This Like a Somthing awesome text with 
   goods:Fb,Teleg,Top,Prods.fm,Ad-...
';

How I can remove spaces and line breaks? I want get this:

$str = 'This Like a Somthing awesome text with goods:Fb,Teleg,Top,Prods.fm,Ad-...';

A tried use:

trim($str);

But get same result.

Mafys Grif
  • 557
  • 3
  • 13
  • 30

1 Answers1

2

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

Nick
  • 138,499
  • 22
  • 57
  • 95