• 0

Rude Worrd Censoring in PHP


Question

Please pardon the vulgarity, but this is only an example. Please examine the code below:

<?
$str = "Joe Cocker is a ###### sucker";
$str = eregi_replace("######"," <I>****</I> ",$str);
echo $str;
?>

I am trying to write a censor for a website that takes a string and removes rude words. I have it working as the example above, but the output is:

"Joe **** er is a **** sucker"

rather than:

"Joe Cocker is a **** sucker"

Can anyone advise how I can correct this?

Cheers

Edited by AshMan
Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

Smifffy, I'm not sure that would work. I think it would do the same has his code does already.

There needs to be a check for the characters before and after. I think this should do the job, although I haven't tested it:

$str = preg_replace( "|(^[a-z])######(^[a-z])|i", "$1****$2", $str );

That should look for the word ###### with anything other than a letter on either (or both) sides.

Link to comment
Share on other sites

  • 0

i think they mean chickens, don't you?

actually, it's best with this:

$output = preg_replace( "#\scock\s#i", "<i>****</i>", $input );

You can also pop an array into the match and replace parameters to do it all in one function call. Works best that way :)

Link to comment
Share on other sites

  • 0
i think they mean chickens, don't you?

actually, it's best with this: 

$output = preg_replace( "#\scock\s#i", "<i>****</i>", $input );

You can also pop an array into the match and replace parameters to do it all in one function call. Works best that way :)

Can you provide an example?

I have other words I have to censor too like...well you can guess I'm sure! ;-)

Edited by AshMan
Link to comment
Share on other sites

  • 0
i think they mean chickens, don't you?

actually, it's best with this:

$output = preg_replace( "#\scock\s#i", "<i>****</i>", $input );

You can also pop an array into the match and replace parameters to do it all in one function call. Works best that way :)

Will that catch things like "######."?

Quite like to see an example of the array system. Never tried that.

Link to comment
Share on other sites

  • 0

actually, lemme modify it for that purpose and show you the array thing at the same time!!!! :o :o

$matches = array( "#\Wcock\W#i", "#\Wmonkey\W#i", "#\Wetcetra\W#i" );
$replacings = array( "<i>****</i>", "<i>******</i>", "<i>******</i>" );
$output = preg_replace( $matches, $replaces, $input );

Hope I didn't mess that up :D

Link to comment
Share on other sites

  • 0

It' works but I've found that it strips a preceding carriage return.

Here is my text exactly:

Joe Cocker sucks

cocks

However, the returned string strips the carriage return preceding the words ######. Here is the output:

Joe Cocker sucks****

Link to comment
Share on other sites

  • 0

Let's try this then:

$matches = array( "#(\W)######(\W)#i", "#(\W)monkey(\W)#i", "#(\W)etcetra(\W)#i" );
$replacings = array( "\\1<i>****</i>\\2", "\\1<i>******</i>\\2", "\\1<i>******</i>\\2" );
$output = preg_replace( $matches, $replaces, $input );

That should be it completely :)

Link to comment
Share on other sites

  • 0
Let's try this then:

$matches = array( "#(\W)######(\W)#i", "#(\W)monkey(\W)#i", "#(\W)etcetra(\W)#i" );
$replacings = array( "\\1<i>****</i>\\2", "\\1<i>******</i>\\2", "\\1<i>******</i>\\2" );
$output = preg_replace( $matches, $replaces, $input );

That should be it completely :)

Thanks. That s sort of does it, however, if only the word ###### is posted, then is does not get censord.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.