SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict Latox's Avatar
    Join Date
    Dec 2008
    Location
    Australia
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Coding a bad language censor

    I have coded a bad language censor by using a function.

    My function looks kind of like this:

    PHP Code:
    function censor($msg) { 
            
    $msg str_replace("sitepoint","****"$msg); 
          return 
    $msg

    That only replaces "sitepoint" with ****

    How would I code it to replace SITEpoint with **** or sItepoint with **** without adding the values individually?

    Thanks in advance
    :-)

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Rudimentary censor.

    PHP Code:
    <?php

    function censor $word ) { return str_repeat'*'strlen$word[0] ) ); }

    $words = array( 'sword''aaword''fffword' );
    $string 'lots of sword words in this aaword stentance...clean it the fffword up... XD';

    $string preg_replace_callback'/\b' join$words'|' ) .'\b/i''censor'$string );
    var_dump$string );
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Using regex can get tricky, but if you match sitepoint with the case insensitive flag, it will replace SITEPOINT, sitepoint, SitePoint, SiTePoInT etc.

  4. #4
    SitePoint Addict Latox's Avatar
    Join Date
    Dec 2008
    Location
    Australia
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That doesn't look like it'd work..

    Just drew this page up quickly to show you how my function currently works:

    http://rulersofchaos.com/dev/censor.php?text=sItepoint - doesn't work

    http://rulersofchaos.com/dev/censor.php?text=sitepoint - works
    :-)

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dyfuCa View Post
    That doesn't look like it'd work..
    But it does work.

    PHP Code:
    <?php

    function censor $str )
    {
        
    $words = array( 'sword''aaword''fffword' );
        
    $words '/\b' join$words'|' ) . '\b/i';

        function 
    callback $word ) {
            return 
    str_repeat'*'strlen$word[0] ) );
        }

        return 
    preg_replace_callback$words'callback'$str );
    }

    $str 'lots of sword words in this aaword stentance...clean it the fffword up... XD';
    var_dumpcensor$str ) );
    outputs:
    lots of ***** words in this ****** stentance...clean it the ******* up... XD
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Addict Latox's Avatar
    Join Date
    Dec 2008
    Location
    Australia
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That seems to work after making some adjustments, thanks alot

    My next question -

    How to integrate into that, so if I wanted to censor "sitepoint" to "google"

    So if anybody wrote sitepoint, it'd show google, within the same function.

    For example:

    My bad language function, also has an advertisement feature.

    If somebody says aaword, it'll show ******

    If somebody says google.com, it'll show rulersofchaos.com

    Thanks
    :-)

  7. #7
    SitePoint Addict Latox's Avatar
    Join Date
    Dec 2008
    Location
    Australia
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was thinking something like this

    PHP Code:
    <?php

    function censor $str ) {
        
    $words = array( 'aaword' );
            
    $str str_replace("sitepoint","google"$str);

        
    $words '/\b' join$words'|' ) . '\b/i';

        function 
    callback $word ) {
            return 
    str_repeat'*'strlen$word[0] ) );
        }
        return 
    preg_replace_callback$words'callback'$str );
    }

    $text $_GET['text'];

    echo 
    censor($text);

    ?>
    Just noticed a problem.
    I have implemented this into my site, but I am getting an error -

    Fatal error: Cannot redeclare callback() (previously declared in /home/roc/public_html/funkcije.php:184) in /home/roc/public_html/funkcije.php on line 184

    This is line 184-187:
    PHP Code:
        function callback $word ) {
            return 
    str_repeat'*'strlen$word[0] ) );
        }
        return 
    preg_replace_callback$words'callback'$str ); 
    :-)

  8. #8
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're declaring callback() in censor() so if you use censor() more than once then it's trying to redeclare callback() hence the fatal error.

    You can declare it as an anonymous function instead (if you don't want to create it in global namespace or use OOP approach):

    PHP Code:
        $callback create_function('$word''return str_repeat( \'*\', strlen( $word[0] ));');

        return 
    preg_replace_callback$words$callback$str ); 

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If you want to search the accumulated knowledge on the forum then this phrase will find some interesting approaches: profanity filter
    Last edited by Cups; Jan 22, 2009 at 03:19. Reason: I wanted to say ******** profanity filter, but managed to reign myself back

  10. #10
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by decowski View Post
    You can declare it as an anonymous function instead (if you don't want to create it in global namespace or use OOP approach):

    PHP Code:
        $callback create_function('$word''return str_repeat( \'*\', strlen( $word[0] ));');

        return 
    preg_replace_callback$words$callback$str ); 
    For completeness, PHP 5.3 closures.
    PHP Code:
        $call = function ( $word ) {
            return 
    str_repeat'*'strlen$word[0] ) );
        }

        return 
    preg_replace_callback$words$call$str ); 
    Last edited by logic_earth; Jan 22, 2009 at 05:12.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  11. #11
    SitePoint Addict Latox's Avatar
    Join Date
    Dec 2008
    Location
    Australia
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    logic, the solution you posted causes unexpected T_FUNCTION errors.

    decowski's solution works, I see no problem with your solution though. :/
    :-)

  12. #12
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Mine only works in PHP 5.3 because it uses a new feature, closures.
    http://wiki.php.net/rfc/closures
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  13. #13
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also worth mentioning that PHP 5.3 is still in Alpha (Beta to be released sometime today).

  14. #14
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    If you're going to use regular expressions to filter the text, you'll want to preg_quote the strings you're matching against. If you want to do a simple case-insensitive replacement, there's always str_ireplace.
    Salathe
    Software Developer and PHP Manual Author.

  15. #15
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    I'm still working with 5.2 but closures looks good. I like the anonymous function too, although I've always wrapped my inner functions in function_exists conditionals, I think it will work nicely for simpler/shorter ones.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •