PHP decoding and fixing content

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 decoding and fixing content

Post by cjkeane »

Hi everyone,
I'm having a few issues which I need to resolve. I'm hoping someone can help me.

When saving some content into the db, htmlspecialchars was used, then othertimes just mysql_real_escape_string was used.
Multiple empty paragraphs were saved, and multiple < br / >'s or nl's were saved.

I need to do the following:
1. make sure the content is utf8 decoded if its encoded
2. display htmlspecialchars correctly
3. remove all empty paragraphs <p> &nbsp; </p>
4. convert nl to < br / >
5. convert < /p > to < br / >
6. remove empty br tags < br > &nbsp; < br / >
7. make sure there are no more than 3 < br / >'s.
8. strip all html tags except < br />< br >< br ><a>

for the most part, my coding displays the content correctly, however there are some instances where &nbsp and other characters appear as �.

this is what i have so far:

Code: Select all

<?php 
        function is_valid_utf8($text) {return (bool)preg_match('//u', serialize($text));}
        $contentfixed = nl2br($result['content'],true);
        $contentfixed = strip_tags($contentfixed, '<br /><br ><br><a>');
        $contentfixed = implode('&nbsp;', array_filter(explode('&nbsp;', $contentfixed)));
	$contentfixed = str_replace("P {margin:0;}", "",  $contentfixed);
	$contentfixed = str_replace("<","<",str_replace(">",">",$contentfixed));
	$contentfixed = str_replace("<br /> &nbsp;<br />", "<br />",  $contentfixed);
	$contentfixed = str_replace("&nbsp;", "", $contentfixed);
	$contentfixed = preg_replace('/(<br[^>]*>\s*){2,}/', '<br/>', $contentfixed);
	if (is_valid_utf8($contentfixed) === true) {
             $contentfixed = utf8_decode($contentfixed);
        } else { 
             $contentfixed = ($contentfixed);
        }
        echo $contentfixed;
?>
Post Reply