Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Location
    Texas
    Posts
    472
    Thanks
    1
    Thanked 0 Times in 0 Posts

    I want to look for censored words

    A while back I asked how to censor words like "hell" without censoring "hello" and someone told me to use preg_replace(). And that worked great. But is there also a similar way I can look for a censored word in a string. Like strpos() but without finding "hell" in "hello", for example?
    If you're reading this, it may already be too late!

  • #2
    New Coder
    Join Date
    Apr 2005
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The string you're looking for is " hell " not "hell"

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Location
    Texas
    Posts
    472
    Thanks
    1
    Thanked 0 Times in 0 Posts
    But what if someone says "go to hell." There would be a period instead of a space.
    If you're reading this, it may already be too late!

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Location
    Texas
    Posts
    472
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Is there a "preg" way of doing this?
    If you're reading this, it may already be too late!

  • #5
    New Coder
    Join Date
    Aug 2004
    Location
    UK
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not sure what the regex would be but you need to censor 'hell' whenever it has anything BUT letters adjacent to it i.e. spaces, full stops etc.

  • #6
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you just want to use regex to check if a pattern exists without manipulating it, use preg_match().

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    preg_*() functions utilize many aspects that people don't use. And I don't know why.
    To start with, what you are looking for is called the word boundary. You only want it to search for 'hell', not 'hello'. You can use a word bound for this. Oh, preg matching/replacing calls this with a \b. But don't hardcode this. The reason why is simple, say now you take a word like @$$ (don't know if its censored here, so we don't want to lose it). If you word boundary that search, it will only find @$$, not @$$hole for instance. So I would recommend creating it so that it stored within your curly braces for bounded words:
    {hell}
    @$$
    for example.
    Then preg replace using boundary's if your curly braces are found, without if not found.
    Hope this gives you an idea of what to look for!

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Location
    Texas
    Posts
    472
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok I got it to work. I was going to use preg_match() but it said I should use strpos. But I used preg_match() anyway.
    If you're reading this, it may already be too late!


  •  

    Posting Permissions

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