Forum Moderators: coopster
[ b ]text[ /b ] (without the spaces inside the tags) is displayed bolded. [ b ] (without spaces) and replace them with <b>, then do the same for every other supported tag. The problem with this is that the user will sometimes write invalid markup, and I don't want that to translate into invalid HTML. Also, if I use a table layout I can't support tables, since a post of only [/table] would break the page layout. [edited by: Skier88 at 8:32 pm (utc) on May 18, 2010]
/<([^\s\/>]+)([^>]+)?>(?m)(.*?)(?-m)<\/\\1>/is
/<\/?([^\s\/>]+)([^>]+)?>/is
Note that I have a database table of "allowed HTML" - I run the open/closed prior to the stand alone, hence the reason why the standalone allows tags to start with </ - it's so I can remove invalid markup entirely.preg_match('/<([^\s\/>]+)([^>]+)?>(?m)(.*?)(?-m)<\/\\1>/is', $input, $out);
$check = 0;
if (in_array($out[1], $allowed_html_closed)) {
// allowed_html_closed is a pre-defined array of allowed tags generated from a database
if(!preg_match('/(onclick|ondblclick|onmousedown|onmousemove|onmouseout|onmouseover|onmouseup|onkeydown|onkeypress|onkeyup|style)/is', $out[2])) {
// The above checks for attributes we don't wan't to allow
if(count(explode('"', $out[2])) % 2) {
// The above makes sure that there is an even number of quotation marks
$check = 1;
}
}
}
if($check == 1) {
// Allow this HTML
} else {
str_replace($out[0], $out[3], $input);
// Removes the HTML, keeps whatever was enclosed by it
}
[edited by: Skier88 at 3:28 pm (utc) on May 21, 2010]