Tuesday, June 4, 2013

Replace special characters when copying from Words in posting in PHP



When we use online editor in PHP, we often found messing when copying text  from Words.
For example,  when I copied text with apostrophe  from Words, all the text after apostrophe was cutoff. While typing apostrophe is fine. The apostrophe in Words is ascii code 145, 146, ie characters chr(145), chr(146).
Below PHP function SanitizeFromWord is to replace special characters from Words during online posting in PHP.

function SanitizeFromWord($Text = '') {

    $chars = array(
        130=>',',     // baseline single quote
        131=>'NLG',   // florin
        132=>'"',       // baseline double quote
        133=>'...',   // ellipsis
        134=>'**',      // dagger (a second footnote)
        135=>'***',      // double dagger (a third footnote)
        136=>'^',       // circumflex accent
        137=>'o/oo',  // permile
        138=>'Sh',      // S Hacek
        139=>'<',      // left single guillemet
        140=>'OE',      // OE ligature
        145=>'\'',      // left single quote
        146=>'\'',      // right single quote
        147=>'"',      // left double quote
        148=>'"',      // right double quote
        149=>'-',      // bullet
        150=>'-',      // endash
        151=>'--',      // emdash
        152=>'~',      // tilde accent
        153=>'(TM)',  // trademark ligature
        154=>'sh',      // s Hacek
        155=>'>',      // right single guillemet
        156=>'oe',      // oe ligature
        159=>'Y',      // Y Dieresis
        169=>'(C)',      // Copyright
        174=>'(R)'      // Registered Trademark
    );
   
    foreach ($chars as $chr=>$replace) {
        $Text = str_replace(chr($chr), $replace, $Text);
    }
    return $Text;
}


Usage:
   $_POST['objectives']      = SanitizeFromWord(trim($_POST['objectives']));

No comments:

Post a Comment