13

Is there a PHP string function that transforms a multi-line string into a single-line string?

I'm getting some data back from an API that contains multiple lines. For example:

<p>Some Data</p>

<p>Some more Data</p>

<p>Even More Data</p>

I assign that data to a variable, then echo the variable as part/"cell" of a CSV document.

It's breaking my CSV document. Instead of all content showing in one cell (when viewing in OpenOffice Calc), it shows in multiple cells and rows. It should be contained within one cell.

I would like to transform the string into:

<p>Some Data</p><p>Some more Data</p><p>Even More Data<p>

Or, what is the best fix for this?

jldupont
  • 93,734
  • 56
  • 203
  • 318
edt
  • 22,010
  • 30
  • 83
  • 118
  • It shouldn't break your document. It's legal to put newlines in a CSV cell value, as long as you correctly double-quote your string. – bobince Nov 09 '09 at 14:55
  • Yes, adding the double quotes worked. – edt Nov 10 '09 at 14:10
  • You might find [`s($str)->normalizeLineEndings('')`](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L540) helpful, as found in [this standalone library](https://github.com/delight-im/PHP-Str). – caw Jul 27 '16 at 03:49

6 Answers6

40

Line conversion techniques

Approach 1

To remove anything unnecessary between the closing and opening </p>...<p> tags you can use a regular expression. I haven't cleaned it up so it's just for reference.

$str = preg_replace("/(\/[^>]*>)([^<]*)(<)/","\\1\\3",$str);

It will strip anything between the p tags, such as newlines, whitespace or any text.

Approach 2

And again with the delete-only-linebreaks-and-newlines approach

$str = preg_replace("/[\r\n]*/","",$str);

Approach 3

Or with the somewhat faster but inflexible simple-string-replacement approach

$str = str_replace(array("\r","\n"),"",$str);

Take a pick!

Comparison

Let's compare my methods

Performance

Performance is always relative to the fastest approach in this case the second one.

(Lower is better)

Approach 1   111
Approach 2   300
Approach 3   100

Result

Approach 1
Strips everything between tags

Approach 2 and 3
Strips newline and linebreak characters

Peter Lindqvist
  • 10,122
  • 3
  • 41
  • 60
6

This will remove line breaks only, you obviously don't want to remove spaces as this would apply to the string within your paragraph tags.

$str = str_replace(array("\n", "\r"), '', $str);
Ben Everard
  • 13,652
  • 14
  • 67
  • 96
  • Thanks, but in the api response, there are no instances "\n" or "\r". I tried your example with no success. What else could be causing the new lines? – edt Nov 09 '09 at 15:25
  • Try the first method from my answer. See if it works, whitespace can be pain sometimes. – Peter Lindqvist Nov 09 '09 at 15:33
2

You simply need to remove all new line (\n) and carriage return (\r) characters from the string. In PHP this is as simple as:

$string = str_replace(array("\n", "\r"), '', $string);
defines
  • 10,229
  • 4
  • 40
  • 56
2

The best method I found to make a multi-line string a single line was like this

$newstring = str_replace(array("\r\n", "\n", "\r"), '', $oldstring);

This is similar to other answers but adds "\r\n", which must be the initial part of the array, so that it doesn't double up.

Richard
  • 909
  • 1
  • 8
  • 13
1

just in case anyone couldn't find a concrete answer, use following method:

$newVariabe = preg_replace('/\s+/', '_', $oldVariable);



source

Ahsaan Yousuf
  • 685
  • 7
  • 16
Craig Wayne
  • 4,499
  • 4
  • 35
  • 50
0

Only a single line instead of multiple with Keep a tab and blank space between text.

$text="
test1
                                               
                test2


                                     tets4

                                     
                        test5
             test6                   test8                           test9
test10
";

$lines = explode("\r\n", $text);
$data="";
foreach($lines as $line)
{
    if($line=="")
    {
        $isline=1;
    }
    else
        $isline=0;

    if($isline==0 && $line!="")
    {
        $data.=$line."\n";
    }   
}
Ramesh
  • 1,495
  • 12
  • 14