PHP function to remove MS Word Characters

There has been many times I have implemented WYSIWYG editors on my clients website only to have proprietary MS tags and code ruin the layout.

I have been using this block of code for quite some time with pretty good success. It could use a bit more testing and I am sure there are additional characters or regular expression rules that could really button it up.

If you find a way to improve upon this code please post in the comments and I will implement them here as necessary.

The Function:

function all_ascii($stringIn){
   $final = '';
   $search = array(chr(145),chr(146),chr(147),chr(148),chr(150),chr(151));
   $replace = array("'","'",'"','"','–','–');

   $hold = str_replace($search,$replace,$stringIn);

       function str_split($string,$split_length=1){
           $count = strlen($string);
           if (($split_length < 1) || ($count  > 1)) {
               return array($string);
           } else {
               $num = (int)ceil($count/$split_length);
               $ret = array();
                   $ret[] = substr($string,$i*$split_length,$split_length);
               return $ret;

   $holdarr = str_split($hold);
   foreach ($holdarr as $val) {
       if (ord($val) < 128) $final .= $val;
   return $final;

Example Usage

echo all_ascii( $myString );

2 Comments + Add Comment

  • str_replace should be able to process arrays so I’m wondering why you wouldn’t just do


    Thumb up 1 Thumb down 0

  • Thanks Tim! Your algorithm works in my code. Other similar algorithms using just the str_replace function I found worked, but then broke other parts of my code that were manipulating the text. Don’t know why, but yours is great!

    Thumb up 0 Thumb down 0

Leave a comment

Show/Hide Footer Actions

Status: Available for your project.