note 64828 added to function.preg-replace

From: Date: Fri, 21 Apr 2006 12:15:54 +0000
Subject: note 64828 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-108917@lists.php.net to get a copy of this message
For those of you that have ever had the problem where clients paste text from msword into a CMS, where word has placed all those fancy quotes throughout the text, breaking the XHTML validator... I have created a nice regular expression, that replaces ALL high UTF-8 characters with HTML entities, such as ’. Note that most user examples on php.net I have read, only replace selected characters, such as single and double quotes. This replaces all high characters, including greek characters, arabian characters, smilies, whatever. It took me ages to get it just downto two regular expressions, but it handles all high level characters properly. $text = preg_replace('/([\xc0-\xdf].)/se', "'&#' . ((ord(substr('$1', 0, 1)) - 192) * 64 + (ord(substr('$1', 1, 1)) - 128)) . ';'", $text); $text = preg_replace('/([\xe0-\xef]..)/se', "'&#' . ((ord(substr('$1', 0, 1)) - 224) * 4096 + (ord(substr('$1', 1, 1)) - 128) * 64 + (ord(substr('$1', 2, 1)) - 128)) . ';'", $text); ---- Server IP: 210.55.230.123 (proxied: 202.89.36.242 1.0 proxy5.akl1.maxnet.net.nz:8080 (Squid/2.4.STABLE2)) Probable Submitter: 202.0.52.59 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- http://master.php.net/note/edit/64828 Del: integrated -- http://master.php.net/note/delete/64828/integrated Del: useless -- http://master.php.net/note/delete/64828/useless Del: bad code -- http://master.php.net/note/delete/64828/bad+code Del: spam -- http://master.php.net/note/delete/64828/spam Del: non-english -- http://master.php.net/note/delete/64828/non-english Del: in docs -- http://master.php.net/note/delete/64828/in+docs Del: other reasons-- http://master.php.net/note/delete/64828 Reject -- http://master.php.net/note/reject/64828 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#108917) next »