tags:

views:

83

answers:

5
+2  Q: 

Filter some words

I want to filter some reserved word on my title form.

$adtitle = sanitize($_POST['title']);
$ignore = array('sale','buy','rent');
if(in_array($adtitle, $ignore)) {
$_SESSION['ignore_error'] = '<strong>'.$adtitle.'</strong> cannot be use as your title';
header('Location:/submit/');
exit;
  • How to make something like this. If user type Car for sale the sale will detected as reserved keyword.
  • Now my current code only detect single keyword only.
+4  A: 

You're probably looking for a regular expression:

foreach($ignore as $keyword) {
  if(preg_match("/\b$keyword\b/i", $adtitle) {
    // Uhoh, the user used a bad word!!
  }
}

This will also prevent some false positives, such as 'torrent' not coming up as a reserved word because it contains 'rent'.

Erik
thank you erik ;)
bob
it should probably be case insensitive, too
Carson Myers
Good point @Carson
Erik
+1  A: 
function isValidTitle($str) {
   // these may want to be placed in a config file
   $badWords = array('sale','buy','rent'); 

    foreach($badWords as $word) {
        if (strstr($str, $word)) return false; // found a word!
    }
    // no bad word found
    return true;

}

If you'd like to match the words only (not partial matches as well, as in within other words), try this modified one below

function isValidTitle($str) {

       $badWords = array('sale','buy','rent'); 

        foreach($badWords as $word) {
            if (preg_match('/\b' . trim($word) . '\b/i', $str)) return false; 
        }

        return true;

    }
alex
Wouldn't that flag the word "torrent" because of "rent"? Thats why I went regex with word boundaries.
Erik
@Erik Yeah but he didn't specify if partial words should be matched or not. He can replace with `preg_match('/\b' . $word . '\b/')` if he'd like to.
alex
should probably be case insensitive also
Carson Myers
@Carson OK, I'll add the flag. Thanks :)
alex
+1  A: 

$adtitle = sanitize($_POST['title']);

$ignoreArr = array('sale','buy','rent');

foreach($ignoreArr as $ignore){
if(strpos($ignore, $adtitle)!==false){

 $_SESSION['ignore_error'] = '<strong>'.$adtitle.'</strong> cannot

be use as your title';

 break;
}  

} header('Location:/submit/');

exit;

This should work. Not tested though.

pinaki
A: 

How about something as simple as this:

if ( preg_match("/\b" . implode("|", $ignore) . "\b/i", $adtitle) ) {
    // No good
}
Justin Johnson
Warning: implode() [function.implode]: will be there
bob
@bob What you you talking about? There is no error.
Justin Johnson
+3  A: 

You could also try something like this:

$ignore = array('sale','rent','buy');
$invalid = array_intersect($ignore, preg_split('{\W+}', $adtitle));

Then $invalid will contain a list of all the reserved words used in the title. This could be useful if you wanted to explain why the title cannot be used.

EDIT:

$invalid = array_intersect($ignore, preg_split('{\W+}', strtolower($adtitle));

if you want case-insensitive matching.

Duncan
+1 Nice solution
Justin Johnson