note 63219 deleted from function.preg-replace by colder
| From: | colder@php.net | Date: | Mon, 18 Jun 2007 20:00:24 +0000 |
| Subject: | note 63219 deleted from function.preg-replace by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127692@lists.php.net to get a copy of this message | ||
Note Submitter: Graham Dawson <graham at imdanet dot com>
----
I said there was a better way. There is!
The regexp is essentially the same but now I deal with problems that it couldn't handle, such
as urls, which tended to screw things up, and the odd placement of a : or ; in the body text, by
using functions. This makes it easier to expand to take account of all the things I know I've
not taken account of. But here it is in its essential glory. Or mediocrity. Take your pick.
<?php
define('PARSER_ALLOWED_STYLES_',
'text-align,font-family,font-size,text-decoration');
function strip_styles($source=NULL) {
$exceptions = str_replace(',', '|',
@constant('PARSER_ALLOWED_STYLES_'));
/* First we want to fix anything that might potentially break the styler stripper, sow e try and
replace
* in-text instances of : with its html entity replacement.
*/
function Replacer($text) {
$check = array (
'@:@s',
);
$replace = array(
':',
);
return preg_replace($check, $replace, $text[0]);
}
$source = preg_replace_callback('@>(.*)<@Us', 'Replacer', $source);
$regexp =
'@([^;"]+)?(?<!'. $exceptions.
')(?<!\>\w):(?!\/\/(.+?)\/|<|>)((.*?)[^;"]+)(;)?@is';
$source = preg_replace($regexp, '', $source);
$source = preg_replace('@[a-z]*=""@is', '', $source);
return $source;
}
?>