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'…)
时,它们是示例中实际使用的一些单词,但已经被编辑掉了。