English 中文(简体)
How to Transform a String from Multi-Line to Single-Line in PHP?
原标题:

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?

最佳回答

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("/[
]*/","",$str);

Approach 3

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

$str = str_replace(array("
","
"),"",$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

问题回答

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("
", "
"),   , $str);

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

$string = str_replace(array("
", "
"),   , $string);

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

$newstring = str_replace(array("
", "
", "
"),   , $oldstring);

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

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

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



source

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("
", $text);
$data="";
foreach($lines as $line)
{
    if($line=="")
    {
        $isline=1;
    }
    else
        $isline=0;

    if($isline==0 && $line!="")
    {
        $data.=$line."
";
    }   
}




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签