Page 1 of 1

php string replace

Posted: Fri Jul 08, 2011 12:09 am
by cjkeane
hi everyone:

i'm not sure how to accomplish this so i'm wondering if anyone has any pointers:

when i import an email into a mysql and then display it on a page, i'm getting content double spaced and for the life of me i can't seem to get rid of them. i need to strip all html except spaces and breaks because some output is saved from outlook and has ms word formatting which screws up the page. when i display content everything displays ok with the following code, except that sometimes the content is double spaced. any ideas?

Code: Select all

<?php
          $content= $result['content'];
	  $fixed = preg_replace('/<!--.*?-->/ms', '', $content); 
	  $fixed = nl2br($fixed);
          echo $fixed;
?>

Re: php string replace

Posted: Fri Jul 08, 2011 10:57 am
by McInfo
My guess is that your content is something like

Code: Select all

The quick brown<br />
fox jumped over<br />
the lazy dog.
Notice that each line is terminated by a <br> tag and an invisible newline. When you use nl2br(), an additional <br> tag is added for each newline, making the content double-spaced.

Code: Select all

The quick brown<br /><br />
fox jumped over<br /><br />
the lazy dog.
See strip_tags().

Re: php string replace

Posted: Fri Jul 08, 2011 11:09 am
by cjkeane
in looking at the page source code, it's not <br> that's causing the breaks, its actually <p class=MsoNormal> </p>
is there a way to replace <p class=MsoNormal> </p> with just <br/>?

Re: php string replace

Posted: Fri Jul 08, 2011 1:41 pm
by McInfo
Can you post a sample of the content? Otherwise, I'm just guessing.

Re: php string replace

Posted: Fri Jul 08, 2011 2:04 pm
by cjkeane
text appears as

the quick brown fox ran across the street

the quick brown fox ran across the street again

the quick brown fox ran across the street and again

when i look at the page source code, i see:
<td><div><p class=MsoNormal>the quick brown fox ran across the street</p><p class=MsoNormal>the quick brown fox ran across the street again</p><p class=MsoNormal>the quick brown fox ran across the street and again</p></div></td>

so what i need to do is replace <p> with <br> so that the text appears as:
the quick brown fox ran across the street
the quick brown fox ran across the street again
the quick brown fox ran across the street and again

any help would be appreciated. thanks.

Re: php string replace

Posted: Fri Jul 08, 2011 4:31 pm
by McInfo
Like this?

Code: Select all

// The input string:
$subject = '<td><div><p class=MsoNormal>the quick brown fox ran across the str'.
    'eet</p><p class=MsoNormal>the quick brown fox ran across the street again'.
    '</p><p class=MsoNormal>the quick brown fox ran across the street and agai'.
    'n</p></div></td>';
// Appends a break tag after every paragraph
$subject = str_replace('</p>', '</p><br />', $subject);
// Strips all HTML tags except <br>
$subject = strip_tags($subject, '<br>');
// The output string:
$subject == 'the quick brown fox ran across the street<br />the quick brown fo'.
    'x ran across the street again<br />the quick brown fox ran across the str'.
    'eet and again<br />';

Re: php string replace

Posted: Fri Jul 08, 2011 5:16 pm
by cjkeane
yes that's exactly as i need. thanks! issue resolved!