php string replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

php string replace

Post 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;
?>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php string replace

Post 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().
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: php string replace

Post 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/>?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php string replace

Post by McInfo »

Can you post a sample of the content? Otherwise, I'm just guessing.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: php string replace

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php string replace

Post 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 />';
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: php string replace

Post by cjkeane »

yes that's exactly as i need. thanks! issue resolved!
Post Reply