SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow Pattern Match/Replace

    I have a small piece of code. All I want it do is find any instances of the word 'hot', and replace it with '###'.

    This is what I have:

    Code Perl:
    $comments = $form_results{'yourcomments'};
     
    print "Content-type=text/html\n\n";
     
    if($comments =~ /hot/) {
    	print "You cannot say the word hot.";
    	$comments =~ s/hot/###/g;
    }
     
    print $comments;

    When I run it, I get a Error 500. I can't see what's wrong with it though?

    Any help is greatly appreciated!

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay I sorted it. Stupid error on my part:

    Content-type=text/html\n\n

    should be:

    content-type: text/html\n\n

    Oh well!

  3. #3
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by james_t_2008 View Post
    I have a small piece of code. All I want it do is find any instances of the word 'hot', and replace it with '###'.

    This is what I have:

    Code Perl:
    $comments = $form_results{'yourcomments'};
     
    print "Content-type=text/html\n\n";
     
    if($comments =~ /hot/) {
    	print "You cannot say the word hot.";
    	$comments =~ s/hot/###/g;
    }
     
    print $comments;

    When I run it, I get a Error 500. I can't see what's wrong with it though?

    Any help is greatly appreciated!
    What about the word "shot" or "hotel"? Your regexp will still reply with "you can't say the word hot" because you are searching for a substring match. Add the \b anchor /\bhot\b/ if you really want to match a word, or leave it as is if you really do want to match substrings.

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi. Thank you for the response!

    That was a good idea thank you! However, if enter "hothothothot", it assumes it's one word, when actually its a repetition of the word I don't want. Is there a way to remove every occurance, even where there are no spaces?

    Thanks!

  5. #5
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by james_t_2008 View Post
    Hi. Thank you for the response!

    That was a good idea thank you! However, if enter "hothothothot", it assumes it's one word, when actually its a repetition of the word I don't want. Is there a way to remove every occurance, even where there are no spaces?

    Thanks!
    You could but you will find this type of filtering is not hard to defeat by the users. For example, instead of 'hot' they will enter 'h0t'.

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh that level of flitering isn't neccesary in this case. How would I go about stopping reoccurances like "hothothothot".

    Thanks!

  7. #7
    Non-Member simjosunny0's Avatar
    Join Date
    Apr 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes level of flitering isn't neccesary in this case.


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
  •