note 64828 deleted from function.preg-replace by cmb
| From: | cmb@php.net | Date: | Fri, 15 May 2015 20:00:37 +0000 |
| Subject: | note 64828 deleted from function.preg-replace by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-202566@lists.php.net to get a copy of this message | ||
Note Submitter: robvdl at gmail dot com
----
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.
<?php
$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);
?>