/    Sign up×
Community /Pin to ProfileBookmark

tightening up a little code…

[code]
<?php
/* Check.php */

function check() /* Establishes the function check */
{
global $text; /* Gets All Variables */

//To Add New Words, Just Copy One Line From Below And Paste It Below The Third Line. Just Replace the first word in quotes with the word you would like, and the second word in quotes to what you would like to change it to.//

$text = str_replace(“hey”,”****”,$text); /* Replaces the word “hey” into “****” */
$text = str_replace(“pie”,”****”,$text);
$text = str_replace(“php”,”***”,$text);

echo $text;

}

return check(); /* Returns the function check() */

?>
[/code]

Following on from an earlyer discussion i found this simple filter out there on the WWW. can anyone help tighten up the code here? am i missing somthing or is one line of code per word a little excessive???

and before people get worried too much about censorship issues the please see my earlyer thread on why i need to impliment this… were not all adults here unfortunatly

As usual Many thanks

Mouse ?

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYNov 13.2005 — about how many words will you need to exclude from content?
Copy linkTweet thisAlerts:
@SheldonNov 13.2005 — This is just a guess and i dont no if it would work

http://nz2.php.net/str_replace

[code=php]
<?php
/* Check.php */

function check() /* Establishes the function check */
{
global $text; /* Gets All Variables */

$words = "pie,beer,lots,keg,breasts";

//To Add New Words, Just Copy One Line From Below And Paste It Below The Third Line. Just Replace the first word in quotes with the word you would like, and the second word in quotes to what you would like to change it to.//

$text = str_replace($words,"****",$text); /* Replaces the word "hey" into "****" */
echo $text;

}

return check(); /* Returns the function check() */

?>[/code]
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYNov 13.2005 — erm? what's that kind of variable?

you'd either have to assign one to each string or put them in an array, however i believe the latter has some kind of size limit, then loop through the list.
Copy linkTweet thisAlerts:
@NogDogNov 14.2005 — I'd do it:
[code=php]
<?php
function censor($text)
{
$badWords = array("bad",
"words",
"list");
if(function_exists("str_ireplace")) # str_ireplace only available in PHP 5
{
$text = str_ireplace($badWords, '****', $text);
}
else # use regexp
{
foreach($badWords as $key => $val)
{
$badWords[$key] = "/$val/i"; # make it a case-insensitive regexp
}
$text = preg_replace($badWords, '****', $text);
}
return($text);
}
# test it:
$text = "This is a test of the Bad Words list.";
echo "<p>Original: '$text'</p>n";
$text = censor($text);
echo "<p>Censored: '$text'</p>n";
?>
[/code]
Copy linkTweet thisAlerts:
@SheldonNov 14.2005 — 
[code=php]
if(function_exists("str_ireplace")) # str_ireplace only available in PHP 5
[/code]
[/QUOTE]


So if you were running PHP 4.x would you just use str_replace?
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYNov 14.2005 — no, he'd use what is in the else statement, meaning, the case-insensitive regex
Copy linkTweet thisAlerts:
@SheldonNov 14.2005 — Ah, now i understand, brain is not in head today. Thanks
Copy linkTweet thisAlerts:
@NogDogNov 14.2005 — Yeah, I want it to be case-insensitive, and str_replace is a bit faster than preg_replace for simple, one-for-one replacements, thus the if/else in my example.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYNov 14.2005 — Has an array a limited size?
Copy linkTweet thisAlerts:
@Mouse77eauthorNov 14.2005 — In answer to a couple of questions both in the posts and elsewhere...

  • 1. No, I am not a cencorshep [sig] freak, just someone who is building a site that will as sure as eggs is eggs attract children even if they not, or are they intended to be, a core audience… and where you find kids you don’t need too much profanity. Would you want your eight year old asking grandma what a cooking funt is over the Sunday lunch?


  • 2. One of the groups of people who this is aimed at is our charming friends who log in and say “great site!!! And if you need VIAGARA (or you have a dire need to have your credit card ripped off) please visit my site. Viagara will be one of those words being “*****”ed or replaced with [Naughty Word].


  • 3. I don’t know how long the list will of words to be looked at will be… I may go for two lists one for those who use viagara and other key spamming friendly words and have them zapped as above, and a second filter for strong self expression i.e. “Cooking Funt” may become “!#@$*?!” at least this way self expression is supported if not quoted.


  • 4. ideally non case sencetive code would be best to save me working round every possible mix (thanks NogDog)


  • And Finally many thanks to you all for your assistance as usual

    Mouse

    :o
    Copy linkTweet thisAlerts:
    @NogDogNov 14.2005 — Has an array a limited size?[/QUOTE]
    At some very large size you will likely exceed the amount of memory PHP is being allowed to use by the server, but you're probably talking thousands or tens of thousands of elements.
    Copy linkTweet thisAlerts:
    @Mouse77eauthorNov 14.2005 — At some very large size you will likely exceed the amount of memory PHP is being allowed to use by the server, but you're probably talking thousands or tens of thousands of elements.[/QUOTE]
    i am only thinking of about 50 or so words in a form return... we can but see. SO what was the final opinion on the way to script this??

    Mouse ?
    Copy linkTweet thisAlerts:
    @LiLcRaZyFuZzYNov 14.2005 — [url=http://www.webdeveloper.com/forum/showpost.php?p=463645&postcount=5]Post #5[/url]
    ×

    Success!

    Help @Mouse77e 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 6.17,
    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: @nearjob,
    tipped: article
    amount: 1000 SATS,

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

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...