Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Transform a String from Multi-Line to Single-Line in PHP?

Tags:

string

php

csv

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?

like image 569
edt Avatar asked Nov 09 '09 14:11

edt


People also ask

How do you convert multiple lines to one line?

Copy and Paste the multiline text. Once copied, click the "convert" option to convert the text into a single line. It takes a while to get you the text in a single line. Once the text is converted into a single line, you can clear the content using the "clear" option.

How can I convert to string in PHP?

The strval() function is an inbuilt function in PHP and is used to convert any scalar value (string, integer, or double) to a string. We cannot use strval() on arrays or on object, if applied then this function only returns the type name of the value being converted. Return value: This function returns a string.

How do I split a string into multiple lines?

You can have a string split across multiple lines by enclosing it in triple quotes. Alternatively, brackets can also be used to spread a string into different lines. Moreover, backslash works as a line continuation character in Python. You can use it to join text on separate lines and create a multiline string.


2 Answers

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);
like image 38
defines Avatar answered Sep 22 '22 06:09

defines


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

like image 126
Peter Lindqvist Avatar answered Sep 22 '22 06:09

Peter Lindqvist