Remove unsafe HTML characters from Word without replacing HTML markup
<p>When copy/paste from Word I end up with a lot of unsafe characters. Instead of find/replace each character individually I thought it would be useful to write a quick PHP script to do this. </p>

<p>When I hit submit with the sample HTML below each of the characters I would like to replace have been replace with a �. What am I doing wrong?</p>

<p>Am I right in thinking that if I use: <code>htmlentities()</code> or <code>htmlspecialchars()</code> this will replace the HTML markup?</p>

<p>Sample HTML block</p>

<pre><code>&lt;p&gt;Nam ’velit metus, vulputate – eget sodales ut, dignissim “vehicula nisi”. Lor’em ipsum dolor sit amet, consectetur adipiscing elit. Nunc pharetra luctus mi, sollicitudin ultrices lacus iaculis sed. Nam aliquam, tortor id sodales scelerisque, est mauri’s adipiscing nunc, a tincidunt tortor elit eget quam. Fusce sagittis arcu ut urna egestas luctus. Aliquam erat volutpat. Suspendisse ut turpis mi. Nulla facilisi. Ut congue porta urna nec semper. Aenean feugiat ante vitae – dui accumsan placerat. Suspendisse aliquet, libero non tempor–  dignissim, arcu nibh luctus magna, eu pellentesq’ue libero eros nec magna. Phasellus non ullamcorper nisi. Aenean sagittis elit ac lorem imperdiet ac consequat sem commodo. Aenean in elit at lectus blandit varius nec in erat. Mauris elementum, turpis eu eleifend pora, quam purus tempor justo, et feugiat tellus mi sed erat.&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;strong&gt;’Pellentesque’&lt;/strong&gt; nec leo cursus ipsum rhoncus volutpat nec eget mi.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;N–am&lt;/strong&gt; quis lectus enim, ac euismod urna.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Donec&lt;/strong&gt; varius massa augue, at feugiat tortor.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;“Duis”&lt;/strong&gt; non massa eget elit euismod pulvinar.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Duis&lt;/strong&gt; bibendum sodales lorem, vel commodo metus volutpat a.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Nu–nc&lt;/strong&gt; pulvinar lacus in nisl dignissim euismod.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;“Nulla”&lt;/strong&gt; tincidunt nulla adipiscing ante aliquet mattis&lt;/li&gt;
    &lt;/ul&gt;


&lt;?php     
/**
 *
 * @param string $unformatted
 * @return string
 */
function format($unformatted) {

    $html = strtolower(trim($unformatted));

    //replace accent characters, forien languages
    $search = array('à','á','â','ã','ä','ç','è','é','ê','ë','ì','í','î','ï','ñ','ò','ó','ô','õ','ö','ù','ú','û','ü','ý','ÿ','À','Á','Â','Ã','Ä','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ñ','Ò','Ó','Ô','Õ','Ö','Ù','Ú','Û','Ü','Ý'); 
    $replace = array('a','a','a','a','a','c','e','e','e','e','i','i','i','i','n','o','o','o','o','o','u','u','u','u','y','y','A','A','A','A','A','C','E','E','E','E','I','I','I','I','N','O','O','O','O','O','U','U','U','U','Y'); 
    $html = str_replace($search, $replace, $html);

    //replace common characters
    $search = array('/(\s\&amp;\s)/i', '/(\s\£\s)/i', '/(\s\$\s)/i'); 
    $replace = array('&amp;amp;', '&amp;pound;', '&amp;dollar;'); 
    $html= preg_replace($search, $replace, $html);

    //replace MS office crap
    $search = array("‘", "’", "”", "“", "–", "…");
    $replace = array("'", "'", '"', '"', "-", "..."); 
    $html= str_replace($search, $replace, $html);

    return $html;
}

if(isset($_POST['clean'])){
    $html = format($_POST['html']);
} 

?&gt;

&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset="utf-8" /&gt;

    &lt;title&gt;HTML Tidy&lt;/title&gt;

    &lt;style type="text/css"&gt;
        body {
            color: #262626;
            background: #f4f4f4;
            font: normal 12px/18px Verdana, sans-serif;
            height: 100%;
        }
        #container {
            width: 760px;
            margin: 40px auto 0 auto;
            padding: 10px 60px;
            border: solid 1px #cbcbcb;
            background: #fafafa;
            -moz-box-shadow: 0px 0px 10px #cbcbcb;
            -webkit-box-shadow: 0px 0px 10px #cbcbcb;
        }
    &lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;div id="container" class="content"&gt;
        &lt;h1&gt;HTML Tidy&lt;/h1&gt;

        &lt;form action="" method="post"&gt;
            &lt;textarea name="html" id="html" rows="20" cols="90"&gt;&lt;?php if(isset($html)){ echo $html; } ?&gt;&lt;/textarea&gt;

            &lt;input type="submit" name="clean" value="Clean" /&gt;
        &lt;/form&gt;
    &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Properties of file
<img src="http://i.stack.imgur.com/KM1QP.jpg" alt="encoding"></p>

<p>Page headers
<img src="http://i.stack.imgur.com/XdFqD.jpg" alt="headers"></p>
