Page 1 of 1

PHP decoding and fixing content

Posted: Tue Nov 25, 2014 9:05 pm
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;
?>