gpt4 book ai didi

php - 如何在数组变量中使用 Stristr

转载 作者:搜寻专家 更新时间:2023-10-31 21:36:37 26 4
gpt4 key购买 nike

我需要检查数组变量中的单词,我需要这样的东西:

$banned = array('word1','word2','word3','word4');


if (stristr($title, $banned) !== false) {

//$title contains a banned word

}else{

//$title not contains any word of $banned variable array

}

最佳答案

<?php
$banned = array('word1','word2','word3','word4');

$hit = false;
foreach ($banned as $banned_item)
{
if (strpos($title, $banned_item) !== false)
{
$hit = true;
break;
}
}

if ($hit)
{
// $title contains a banned word
}
else
{
//$title not contains any word of $banned variable array
}

_

               ************** update1  ************** 

上面的代码是区分大小写的,如果你想让你的代码不区分大小写,只需更改:

  if (strpos($title, $banned_item) !== false)

到:

  if (stristr($title, $banned_item) !== false)

关于php - 如何在数组变量中使用 Stristr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17781731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com