Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将函数应用于更多变量_Php_Function_Variables_Eregi - Fatal编程技术网

Php 将函数应用于更多变量

Php 将函数应用于更多变量,php,function,variables,eregi,Php,Function,Variables,Eregi,新手问题 我有以下功能: 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 此函数仅适用于变量$des

新手问题

我有以下功能:

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
此函数仅适用于变量
$description
,如果我想将此函数也应用于变量
$title
如何修改函数? 不推荐使用
eregi
如何使用
preg\u match
更改eregi

谢谢


新手应该阅读文档;)

您可以向函数发送
$description
$title
变量。函数声明中提供的变量名仅指函数作用域内的变量名,而不是函数外的任何变量名,即使它们恰好相同(在您的情况下与
$description
相同)

尝试
if(isadult($title)){
,您会发现它会起作用

外部变量
$title
的值放入函数变量
$description
,在函数范围内(在{和}之间)称为
$description


因为在您的示例中没有进行任何实际的正则表达式匹配,所以您可以将
eregi()
替换为
stripos()
stristr()
或类似内容

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

您还应该立即将坏单词列表定义为数组,而不是让php对其进行转换

$bad = array('badword1','badword2','badword3');

只需第二次调用该函数?例如:
if(isadult($description)| | isadult($title))…
为什么要使用
$bad=explode(“,”,$bad);
当您可以使用
$bad=array('word1','word2'…)
时,它们是示例中实际使用的一些单词,但已经被编辑掉了。