/    Sign up×
Community /Pin to ProfileBookmark

message filtering

Ok I’ve made a calendar, blog, and forum(basiclly a CMS). It’s not completed(far from) but its a work in progress…anyways to the post. I’ve got a function started to filter the messages. As of right now, i have everything being put in the message sent to the db w/o filtering, and i have the filter apply when it’s displayed. My gut feeling is that it should be applied when submitting it to the database should i be trusting my gut?

the second question is what else should i add to the function? right now i have what i got off of php.net documentation for preg_replace(their example), bad words, BBC filter, and nl2br() is in there. I’d like to have a limit on text(much like wordwrap) but i dont want to use wordwrap because(if i understand it right) will wrap the text no matter what, at the specified length…i want it to only apply to words greater then X characters…

[code=php]function filter($message) {
$char_limit = “30”;
$add = “…”;
//Take care of html, scripts, whitespace
$bad_stuff = array (‘@<script[^>]*?>.*?</script>@si’, // Strip out javascript
‘@<[/!]*?[^<>]*?>@si’, // Strip out HTML tags
‘@<[/!]*?[^<>]*?@si’, // Strip out invalid HTML tags
‘@&(quot|#34);@i’, // Replace HTML entities
‘@&(amp|#38);@i’,
‘@&(lt|#60);@i’,
‘@&(gt|#62);@i’,
‘@&(nbsp|#160);@i’,
‘@&(iexcl|#161);@i’,
‘@&(cent|#162);@i’,
‘@&(pound|#163);@i’,
‘@&(copy|#169);@i’,
‘@&#(d+);@e’); // evaluate as php

$bad_stuff_replace = array (”,
”,
”,
‘”‘,
‘&’,
‘<‘,
‘>’,
‘ ‘,
chr(161),
chr(162),
chr(163),
chr(169),
‘chr(1)’);

$message = preg_replace($bad_stuff, $bad_stuff_replace, $message);
//Great, that’s done

//Now lets get rid of them bad words
$bad_words = array (“/crap/i”,
“/crap/i”,
“/****/i”);
$bad_words_replace = “images/emoticons/censored.gif”;
$message = preg_replace($bad_words, $bad_words_replace, $message);
//Great!Bad words done.

//Next lets take care of html/bbc code
$BBcode = array (“/[img](.*?)[/img]/si”,
“/[url=(.*?)](.*?)[/url]/si”,
“/[url](.*?)[/url]/si”,
“/[([biu])]/i”,
“/[/([biu])]/i”);
$BBcode_replace = array (“<img src=”\1″ />”,
“<a href=”http://\1″ title=”\2″ target=”_blank”>”. (strlen(“\2”)>=$char_limit ? substr(“\2”,0,$char_limit) . “$add”:”\2″) .”</a>”, //not sure why the limit isn’t working
“<a href=”http://\1″ title=”\1″ target=”_blank”>”. (strlen(“\1”)>=$char_limit ? substr(“\1”,0,$char_limit) . “$add”:”\1″) .”</a>”, //doesn’t work here either
“<\1>”,
“</\1>”);
$message = preg_replace($BBcode, $BBcode_replace, $message);
//Cool, html/bbc taken care of

//This time put breaks if an enter was put in
$message = nl2br($message);
//Great, that’s done

return($message);
}//End the function[/code]

Any suggestions is appriciated. If you have any suggestions, posting code isn’t neccasary(as i’m not paying any of u guys to do this) but it’d be greatly appriciated.

Thanks,
Dustin

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@the_treeAug 14.2005 — Yeh I'd trust your gut. However, might I suggest you use str_ireplace? My gut says it'd be neater.
[code=php]<?php
#shortend example

$badstuff = array(
'<',
'>',
'&'
'crap'
);

$goodstuff = array(
'&lt;',
'&gt;',
'&amp;'
'poo'
);

str_ireplace($badstuff,$goodstuff,$string);

?>[/code]
Copy linkTweet thisAlerts:
@GenixdeaeauthorAug 15.2005 — arite, thanks for the info ?
×

Success!

Help @Genixdeae spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...