AshMan Posted January 23, 2003 Share Posted January 23, 2003 (edited) 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 January 24, 2003 by AshMan Link to comment Share on other sites More sharing options...
0 Ricky Baby Posted January 23, 2003 Share Posted January 23, 2003 use a preg_replace (REGEX) <?php $output = preg_replace( "|######|is", "<i>****</i>", $input ); ?> Link to comment Share on other sites More sharing options...
0 Quboid Posted January 24, 2003 Share Posted January 24, 2003 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 More sharing options...
0 tomwarren Veteran Posted January 24, 2003 Veteran Share Posted January 24, 2003 All this talk about cocks :rofl: Sorry for me interrupting ;) Link to comment Share on other sites More sharing options...
0 CarbonKnight Posted January 24, 2003 Share Posted January 24, 2003 Link to comment Share on other sites More sharing options...
0 Tim Dorr Veteran Posted January 24, 2003 Veteran Share Posted January 24, 2003 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 More sharing options...
0 AshMan Posted January 24, 2003 Author Share Posted January 24, 2003 (edited) 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 January 24, 2003 by AshMan Link to comment Share on other sites More sharing options...
0 Quboid Posted January 24, 2003 Share Posted January 24, 2003 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 More sharing options...
0 Tim Dorr Veteran Posted January 24, 2003 Veteran Share Posted January 24, 2003 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 More sharing options...
0 AshMan Posted January 24, 2003 Author Share Posted January 24, 2003 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 More sharing options...
0 Tim Dorr Veteran Posted January 24, 2003 Veteran Share Posted January 24, 2003 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 More sharing options...
0 AshMan Posted January 24, 2003 Author Share Posted January 24, 2003 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 More sharing options...
Question
AshMan
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 AshManLink to comment
Share on other sites
11 answers to this question
Recommended Posts