[KLUG Programming] PHP, MySQL and MS Word Cut-n-Paste

Adam Tauno Williams adam at morrison-ind.com
Fri Jul 29 08:01:04 EDT 2005


> > >>Does anyone know of a way to strip the MS Word cruft out of a cut and 
>  > >>paste?  Document formating seems to be creeping into a MySQL database, 
> > >>when pasting from MS Word into a PHP form.
> > > Are you literally cutting/copying out of MS Word?
> > Yep

I've faced this before.  The only reliable way to clean this up is to
create an array of all 'printable' (safe) characters.  And then walk
through your input char-by-char calling in_array();

p-code (assuming your input is ASCII) -

function srubMeBaby(string input) {
  array safe_elements[] = new array()
  string output = new string();
  safe_elements.push(chr(9)); //horizontal tab
  safe_elements.push(chr(10)); //newline
  for (int i = 32; i < 127; i++)
    safe_elements.push(chr(i));
  for (int i = 0; i < input.Length; i++)
    if (safe_elements.Contains(input[i]))
      output.push(input[i]);
  return output;
 }




More information about the Programming mailing list