0

I have a string like this:

<p>Here some text
another line</p>

<p>Another paragraph
another line</p>

What I want is something like following :

<p>Here some text another line</p>

<p>Another paragraph another line</p>

Basically I want to remove new line character from the string within paragraph tags not anywhere else.

Please help. Thanks in advance.

Vivek Vaghela
  • 1,075
  • 9
  • 16
  • 2
    [The pony he comes....](http://stackoverflow.com/a/1732454/554546) –  Feb 01 '12 at 15:33
  • What is the reason for doing so? – Felix Kling Feb 01 '12 at 15:33
  • @FelixKling The reason for this is that I want to remove paragraph tags from the string and need to use it somewhere else. So former code in a html page will not show the line break inside paragraph tag. but it will be shown in a textarea. But I want to show this string as it's shown on html page in textarea without the paragraph tags. Hope you are getting me. – Vivek Vaghela Feb 01 '12 at 15:39
  • 1
    Have you looked into [Tidy](http://php.net/tidy)? –  Feb 01 '12 at 18:22

3 Answers3

1

You want to use preg_replace.

$string = "<p>Here some text
another line</p>

<p>Another paragraph
another line</p>


<p>test



123</p>
blah";
$pattern = '/<p>([^\n]+)\n+([^\n]+)/iS';
$replacement = "<p>$1 $2";

$paragraphs = explode("</p>",$string);

for ($i = 0; $i < count($paragraphs); $i++) {
    $paragraphs[$i] = preg_replace($pattern, $replacement, $paragraphs[$i]);
}

$string = implode("</p>", $paragraphs);

echo $string . "\n";
devNoise
  • 364
  • 3
  • 12
1

devNoise, your code works great unless there are multiple lines \n. I'm sure there's a better way to do this with regexp than what I'm posting here, but here it is:

    $string = "<p>Here some text
    another line</p>

    <p>Another paragraph
    another line</p>


    <p>test



    123</p>";

    $paragraphs = explode("</p>",$string);

    for ($i=0;$i<strlen($string);$i++) {
            if (strstr($string[$i].$string[$i+1].$string[$i+2],"<p>")) {
                    $replace_on = 1;
            }

            if (strstr($string[$i].$string[$i+1].$string[$i+2].$string[$i+3],"</p>")) {
                    $replace_on = 0;
            }

            if ($replace_on==1) {
                    if (strstr($string[$i],"\n")) {
                            $new_string .= " ";
                    } else {
                            $new_string .= $string[$i];
                    }
            } else {
                    $new_string .= $string[$i];
            }
    }

    echo $new_string;
seanbreeden
  • 6,104
  • 5
  • 36
  • 45
1

This should do what you want:

If text only contains paragraphs

$string = "<p>Here some text
another line</p>

<p>Another paragraph
another line</p>";

$output = '';

$paragraphs = array_filter(explode("</p>", $string));

foreach ($paragraphs as $block) {
    $output .= str_replace(array("\r", "\n"), '', $block);       
}

var_dump($output); //<p>Here some textanother line</p><p>Another paragraphanother line</p>

If text contains multiple tags

$string = "<p>Here some text 

another line</p>

<img src=\"/path/to/file.jpg\";

<div id=\"test\"> 
   <p>Another paragraph 



another line</p>
</div>";

preg_match_all('~<p>(.+?)</p>~si', $string, $paragraphs);

foreach ($paragraphs[0] as $block) {
    $output = '';

    if (strlen($block)) {
        $output = str_replace(array("\r","\n"), '', $block);
        $string = str_replace($block, $output, $string);
    }
}
Jason Brumwell
  • 3,482
  • 24
  • 16