Noskiw Posted October 4, 2009 Share Posted October 4, 2009 sorry for the swears in this code, but i need some help with the smiley filter.. <?php function censor($string){ if($string){ $sweararray = array('Fuck', 'bloody', 'shit'); $replacearray = array('F**k', 'b****y', 's**t'); $smilearray = array(''); $smileyarray2 = array('<img src=\'http://localhost/images/P.png\' width=\'60\' height=\'60\' />'); $new_string = str_ireplace($sweararray, $replacearray, $string); $new_string2 = str_replace ($smilearray, $smileyarray2, $string); return $new_string && $new_string2; } } if($_POST['submit']){ echo censor($_POST['text']); } ?> <hr /> <form action="swearfilter.php" method="POST"> <textarea name="text"></textarea><br /> <input type="submit" name="submit" value="Filter" /> </form> i can't get the smiley array to work... Quote Link to comment https://forums.phpfreaks.com/topic/176445-swearsmileyfilter/ Share on other sites More sharing options...
Noskiw Posted October 4, 2009 Author Share Posted October 4, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/176445-swearsmileyfilter/#findComment-930083 Share on other sites More sharing options...
cags Posted October 4, 2009 Share Posted October 4, 2009 The input string definately contains a colon immediately by a capital letter p? Theres nothing wrong with your code in as much as $new_string2 will contain the <img tag if $string contains ''. Though I would have thought what you really want is more along the lines of... $string = str_ireplace($sweararray, $replacearray, $string); $string = str_replace ($smilearray, $smileyarray2, $string); ... though bare in mind that it could cause some 'kooky' behavior if any of the swearword $replacearray contains the characters used in $smilearray. It would make more sense (IMHO) to put the smileys in the same arrays as the swear words and just do a single replace. Quote Link to comment https://forums.phpfreaks.com/topic/176445-swearsmileyfilter/#findComment-930087 Share on other sites More sharing options...
nrg_alpha Posted October 4, 2009 Share Posted October 4, 2009 One caveat about censorship... be aware of the Clbuttic mistake. (Clbuttic derives from censoring say 'ass' with 'butt', but as a result, a word like Classic becomes Clbuttic). I would strongly consider making use of word bounderies (\b) in regex patterns for this sort of thing.. otherwise you may well end up censoring parts of legit non profanity words. **digs up older code** Where $txt is the body of text in question, here is what I did (quite a while back) for matters like this. Example: $txt = preg_replace(array('#(\b)?fuck(?:ers?)?(?(1)\b)#i', '#\bbitch(?:y|es)?\b#i', '#\bbastard\b#i', '#(\b)?asshole(?(1)\b)#i', '#(\b)?whore(?(1)\b)#i', '#(\b)?cock(?(1)\b)#i', '#\banal\b#i', '#\banus\b#i', '#(\b)?cunt(?(1)\b)#i', '#(\b)?penis(?(1)\b)#i', '#\bshit(?:[ys]|ty|t?er|t?ier?|t?iest)?\b#i', '#\bslut\b#i','#(\b)?dicks?(?(1)\b)#i', '#\bpuss(?:y|ies?)\b#i', '#\bcum\b#i', '#\bfag(?:s|gots?)?\b#i', '#\bass\b#i', '#\bbullshit(?:t?er)?\b#i', '#\bpricks?\b#i', '#\btit(?:s|ty|ties?)?\b#i', '#\basswipes?\b#i'), '*', $txt); Granted, this replaces the entire word with an asterisk.. and again, the code's a tad on the old side (it was at this point in time I was still learning regex - ok, I mean, there is always more to learn, so I am STILL learning regex, but you know what I mean.. so I was also just getting the hang of regex conditionals and tested that out - the point of using conditionals is to catch a word that is either by itself, or in combination with another one, like whore, or whoreface for example), but seems to do the trick. It takes into account word combinations as well (like bitch, bitch(y), bitch(es) kind of thing..). This code is by no means 'bulletproof'. So I reserve the right to be wrong You may find words that this thing missed, but could be used as a starting point. You would have to muck around to make modifications if you want to change the end results if the current setup is not desirable. Quote Link to comment https://forums.phpfreaks.com/topic/176445-swearsmileyfilter/#findComment-930112 Share on other sites More sharing options...
Noskiw Posted October 4, 2009 Author Share Posted October 4, 2009 thanks for the code, but i was kind of hoping for more help with the smiley face part... I don't understand whats wrong with the colon. The output is just a simple "1"... Quote Link to comment https://forums.phpfreaks.com/topic/176445-swearsmileyfilter/#findComment-930145 Share on other sites More sharing options...
ProXy_ Posted October 4, 2009 Share Posted October 4, 2009 Ok, this might help you out a little bit. i made an online community site once and used this. it works great You had some kind of syntax like... echo '<a href=\'syntax\'>'; Proper syntax: echo "<a href=\"syntax\">"; The reason we're using the backslashes anyway is for php to allow the " symbol. so if your not using the " symbol why use the backslash syntax? I had to remove the function for your smilies the code wasn't displaying here.. so you can see it here http://ju.nu/smileyphp Quote Link to comment https://forums.phpfreaks.com/topic/176445-swearsmileyfilter/#findComment-930152 Share on other sites More sharing options...
cags Posted October 4, 2009 Share Posted October 4, 2009 If you wanted to use the more 'standard' double quotes for the HTML, why not use what is (arguably) faster and certainly easier PHP of... echo '<a href="syntax">'; Quote Link to comment https://forums.phpfreaks.com/topic/176445-swearsmileyfilter/#findComment-930166 Share on other sites More sharing options...
nrg_alpha Posted October 4, 2009 Share Posted October 4, 2009 If you wanted to use the more 'standard' double quotes for the HTML, why not use what is (arguably) faster and certainly easier PHP of... echo '<a href="syntax">'; The keyword here is indeed (arguably), as I think the speed of output when comparing double to single quotes is dependant on which version of PHP you're using. I think 5.2.x has single quotes executing faster.. but in the following test in 5.3, double quotes seems faster: $loop = 50000; $time_start = microtime(true); for($a = 0; $a < $loop; $a++){ echo "<a href=\"syntax\"></a>"; } $time_end = microtime(true); $elapsed_time1 = round($time_end-$time_start, 4); $time_start = microtime(true); for($a = 0; $a < $loop; $a++){ echo '<a href="syntax"></a>'; } $time_end = microtime(true); $elapsed_time2 = round($time_end-$time_start, 4); echo "Using double quotes: " . $elapsed_time1 . "<br />\n"; echo "Using single quotes: " . $elapsed_time2; Run the test a couple of times to see a better average of what numbers are being reported. Granted, in real world applications, the speed difference is going to be infinitesimal, I'm sure. Notice the loop count... not much of a difference when all is said and done. Quote Link to comment https://forums.phpfreaks.com/topic/176445-swearsmileyfilter/#findComment-930219 Share on other sites More sharing options...
cags Posted October 4, 2009 Share Posted October 4, 2009 Indeed, which is why I included the word arguably. Regardless of speed I personally prefer the single quote approach in this case because it saves you escaping the double quotes for the HTML attributes. I have PHP 5.2.9, when running your test code the single quotes were between 0.05 and 0.1 secs faster. As we're talking about 50,000 iterations hardly worth worrying about. I was simply pointing out that if ProXy_ felt the need to correct the OP's usage, that there was IMO a better way. At the end of the day it's all down to personal preference anyway, they all do the same job. Quote Link to comment https://forums.phpfreaks.com/topic/176445-swearsmileyfilter/#findComment-930223 Share on other sites More sharing options...
ProXy_ Posted October 4, 2009 Share Posted October 4, 2009 i agree 100%. i've found myself actually to go from one another. i don't know if its my problem of a.d.d, or i just like to switch things up a bit but i've always had the same output. however: <a href=\'blah\'> i've never seen this syntax used before with php. so i was just helping debug it and name off some possibilities. to maybe solve the problem. Quote Link to comment https://forums.phpfreaks.com/topic/176445-swearsmileyfilter/#findComment-930236 Share on other sites More sharing options...
mikesta707 Posted October 4, 2009 Share Posted October 4, 2009 you can't return multiple values like that in PHP. if you want to return multiple values, put them in an array, and use the list function, like function censor($string){ if($string){ $sweararray = array('Fuck', 'bloody', 'shit'); $replacearray = array('F**k', 'b****y', 's**t'); $smilearray = array(''); $smileyarray2 = array('<img src=\'http://localhost/images/P.png\' width=\'60\' height=\'60\' />'); $new_string = str_ireplace($sweararray, $replacearray, $string); $new_string2 = str_replace ($smilearray, $smileyarray2, $string); return array($new_string,$new_string2); } } list($words, $smiles) = censor($someString); however, since there is only one string being passed in, why would you want two strings being passed out? function censor($string){ if($string){ $sweararray = array('Fuck', 'bloody', 'shit'); $replacearray = array('F**k', 'b****y', 's**t'); $smilearray = array(''); $smileyarray2 = array('<img src=\'http://localhost/images/P.png\' width=\'60\' height=\'60\' />'); $new_string = str_ireplace($sweararray, $replacearray, $string); $new_string = str_replace ($smilearray, $smileyarray2, $new_string); return $new_string; } } btw thanks for the code, but i was kind of hoping for more help with the smiley face part... I don't understand whats wrong with the colon. The output is just a simple "1"... It returns that because this return statement return $new_string && $new_string2; is returning a boolean statement. this look familiar if ($new_string && $new_string2){ it returns 1 because that boolean expression is true Quote Link to comment https://forums.phpfreaks.com/topic/176445-swearsmileyfilter/#findComment-930267 Share on other sites More sharing options...
mikesta707 Posted October 4, 2009 Share Posted October 4, 2009 Ok, this might help you out a little bit. i made an online community site once and used this. it works great You had some kind of syntax like... echo '<a href=\'syntax\'>'; Proper syntax: echo "<a href=\"syntax\">"; The reason we're using the backslashes anyway is for php to allow the " symbol. so if your not using the " symbol why use the backslash syntax? I had to remove the function for your smilies the code wasn't displaying here.. so you can see it here http://ju.nu/smileyphp Both of those are perfectly fine. your "proper" syntax is "arguably" no better than what he had. you dont use the backslash to allow " symbols. you use it to escape symbols. For example the new line character \n or the tab character \t. inside strings surrounded with double quotes you need to escape any double quotes that are inside the string. same for single quotes. you can also do this echo '<a href="syntax">'; as cags said Quote Link to comment https://forums.phpfreaks.com/topic/176445-swearsmileyfilter/#findComment-930269 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.