View Full Version : I want to look for censored words

07-11-2005, 05:57 PM
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?

[m] at
07-11-2005, 05:57 PM
The string you're looking for is " hell " not "hell"

07-11-2005, 05:59 PM
But what if someone says "go to hell." There would be a period instead of a space.

07-11-2005, 07:34 PM
Is there a "preg" way of doing this?

07-11-2005, 07:49 PM
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.

Kid Charming
07-11-2005, 08:07 PM
If you just want to use regex to check if a pattern exists without manipulating it, use preg_match().

07-11-2005, 08:34 PM
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:
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!

07-11-2005, 08:53 PM
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.