-2

Newbie question

i have following function:

function isadult($description)
{
$bad=/*comma separated bad words*/;
$bad=explode(",",$bad);
foreach($bad as $key)
{
if(eregi($key,$description))
{
return true;
}
}
return false;
}  


if (isadult($description)) exit;
else

this function apply only to variable $description if i want apply this function also to variable $title how to modify function ? and eregi is deprecated how to change eregi with preg_match ?

thanks

codaddict
  • 445,704
  • 82
  • 492
  • 529
grigione
  • 697
  • 2
  • 11
  • 37
  • just call the function a second time?like: `if (isadult($description) || isadult($title)) ...` – Yoshi May 30 '11 at 07:42
  • why using `$bad=explode(",",$bad);` when you can use `$bad = array('word1','word2'...)` – k102 May 30 '11 at 07:51

2 Answers2

1

http://www.php.net/manual/en/functions.arguments.php

http://www.php.net/manual/en/function.preg-match.php

newbies should read the doc ;)

MatTheCat
  • 18,071
  • 6
  • 54
  • 69
1

You can send both the $description and the $title variable to the function. The variable name provided in the function declaration is only refering to the variable name within the function scope, and not any variable names outside the function, even if they happen to be identical (as with $description in your case).

Try if(isadult($title)) { and you will find that it will work.

The value of the external variable $title is put into the function variable $description, and will be referred to as $description within the function scope (between { and })


Since you're not doing any actual regular expression matching in your example, you could just as well replace eregi() with stripos(), stristr() or similar...

if(stripos($description, $key) !== false) {

You should also define your bad words list as an array right away, instead of having php converting it.

$bad = array('badword1','badword2','badword3');
Ivar Bonsaksen
  • 4,747
  • 2
  • 32
  • 34