Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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_Arrays - Fatal编程技术网

Php 过滤一些单词

Php 过滤一些单词,php,arrays,Php,Arrays,我想在我的标题表上过滤一些保留字 $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:/subm

我想在我的标题表上过滤一些保留字

$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;
$adtitle=sanitize($_POST['title']);
$ignore=array('sale','buy','rent');
if(在数组中($adtitle,$ignore)){
$\u会话['ignore\u error']='。$adtitle.不能用作您的标题';
标题('位置:/submit/');
出口
  • 如何制作这样的东西。如果 用户类型
    待售汽车
    待售汽车 将检测为
    保留关键字
  • 现在我的当前代码只检测单个关键字

您可能正在寻找正则表达式:

foreach($ignore as $keyword) {
  if(preg_match("/\b$keyword\b/i", $adtitle) {
    // Uhoh, the user used a bad word!!
  }
}
这也将防止一些误报,例如“torrent”不作为保留字出现,因为它包含“rent”

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;

}
如果您只想匹配单词(也不想在其他单词中进行部分匹配),请尝试下面这个修改过的单词

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;

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

$ignoreArr= 数组('sale','buy','rent')

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

} 标题('位置:/submit/')

退出


这应该行得通。不过没有经过测试。

像这样简单的东西怎么样:

if ( preg_match("/\b" . implode("|", $ignore) . "\b/i", $adtitle) ) {
    // No good
}

您也可以尝试以下方法:

$ignore = array('sale','rent','buy');
$invalid = array_intersect($ignore, preg_split('{\W+}', $adtitle));
然后$invalid将包含标题中使用的所有保留字的列表。如果您想解释为什么不能使用标题,这可能会很有用

编辑

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

如果您需要不区分大小写的匹配。

这不是因为“rent”而标记“torrent”这个词吗?这就是我使用带单词边界的正则表达式的原因。@Erik是的,但他没有指定是否应匹配部分单词。他可以替换为
preg\u match('/\b'.$word.\b/'))
如果他愿意的话。应该不区分大小写also@Carson好的,我将添加标志。谢谢:)它可能不区分大小写,所以如果用户输入$keyword,例如*滥用*警告:inpode()[function.inpode]:会发生什么
$ignore = array('sale','rent','buy');
$invalid = array_intersect($ignore, preg_split('{\W+}', $adtitle));
$invalid = array_intersect($ignore, preg_split('{\W+}', strtolower($adtitle));