简体   繁体   English

PHP-正则表达式需要帮助

[英]PHP - Regular Expressions Help Needed

I got a contact form, I need to filter some words. 我有联系表,我需要过滤一些单词。

I'm doing it as following: 我这样做如下:

$array = array('lorem', 'ipsum', 'ip.sum');
for($i = 0; $i < count($array); $i++)
        {
            if( preg_match("/".$array[$i]."/", (string) $field) )
            {
                return false;
            }
        }

I'm not a regex master, but it should be working for words like: lorem or ipsum. 我不是正则表达式大师,但它应该适用于lorem或ipsum之类的单词。 But it is not. 但事实并非如此。

BTW. BTW。 Any suggestions how to catch mispelled words, ex. 关于如何捕获拼写错误的单词的任何建议,例如。 i.psum, lorem? i.psum,lorem?

Update 更新
Of course, I have no empty pattern, I just forgot to paste it. 当然,我没有空模式,只是忘了粘贴它。

Update 2 更新2
I've decided to got the way suggested by Daniel Vandersluis . 我已决定采用Daniel Vandersluis提出的建议。 Abnyway, I'm not able to make it working. Abnyway,我无法使其正常运行。

$field = "ipsum lorem"; // This value comes from textarea
$array = array('ipsum', 'lorem', 'ip.sum');
foreach($array as $term):
    if(preg_match('/'.preg_quote($term).'/', $field)) {
        return false;
    }
endforeach;

Any ideas? 有任何想法吗?

If I understand correctly, and you want to see if any of the words in your array are in your field, you can do something like this: 如果我理解正确,并且想查看数组中是否有任何单词在字段中,则可以执行以下操作:

function check_for_disallowed_words($text, $words)
{
  // $text is the text being checked, $words is an array of disallowed words
  foreach($words as $word)
  {
    if (preg_match('/' . preg_quote($word) . '/', $text))
    {
      return false;
    }
  }

  return true;
}

$array = array('lorem', 'ipsum', 'ip.sum');
$valid = check_for_disallowed_words($field, $array);

In your example, you weren't defining any pattern to be used. 在您的示例中,您没有定义要使用的任何模式。 preg_quote will take a string and make it ready to use in a regular expression (because, for example, the dot in ip.sum actually has special meaning in a regular expression so it needs to be escaped if you want to search for a literal dot). preg_quote将接受一个字符串并使其准备在正则表达式中使用(例如,因为ip.sum的点在正则表达式中实际上具有特殊含义 ,因此如果要搜索文字点,则需要转义该字符串)。

As an aside, if you'd like to learn more about regular expressions, take a look at the tutorial on regular-expressions.info, it is very in depth. 顺便说一句,如果您想了解有关正则表达式的更多信息,请看一下regular-expressions.info上的教程 ,它非常深入。

You don't need regexes for simple word filtering. 您不需要正则表达式即可进行简单的单词过滤。

function is_offensive($to_be_checked){
   $offensive = array('lorem', 'ipsum', 'ip.sum');
   foreach($offensive as $word){
      if(stristr($to_be_checked, $word) !== FALSE){
          return FALSE;
      }
   }
}

Usage: 用法:

$field = $_POST['field'];
if(is_offensive($field)){
   echo 'Do not curse on me! I did not crash your computer!';
}
else{
    //make the visitor happy
}

I translated your question for me like this: how can I replace words from a variable via set of regular expressions. 我这样为我翻译了您的问题: 我如何通过一组正则表达式替换变量中的单词。

You can try this: 您可以尝试以下方法:

 $array = array('lorem', 'ipsum', 'ip.sum', '');

 $field = preg_replace("/(" . implode(")|(", $array) . ")/i", "--FILTERED-OUT--", (string) $field));

It constructs the final regular expression from elements of $array . 它根据$array元素构造最终的正则表达式。 So that you can specify a word as regular expression (ip.sum ~ ip[whatever character]sum). 这样就可以将单词指定为正则表达式(ip.sum〜ip [whatever character] sum)。 Flag i is used for case-insensitive search. 标志i用于不区分大小写的搜索。

Change 更改

if( preg_match("//", (string) $field) )

to

if( preg_match("/$array[$i]/", (string) $field) )

Another variant, maybe that's of some use (you didn't specify the problem very thoroughly): 另一个变体,也许有一定用处(您没有非常彻底地说明问题):

Edited according to user's comment: 根据用户评论编辑

 // comparison function
 function check_field_in($field, $phrases)
{
 foreach($phrases as $phrase) {
    $match_text = quotemeta($phrase);            // if this works, 
    if( preg_match("/^$match_text$/", $field) )  // this part can be optimized
       return false;                             
 }
 return true;
}

// main program goes here
 $textarea = 'lorem ipsum  i.psum l.o.rem';

 foreach(preg_split('/\s+/', $textarea) as $field) {
    if( check_field_in( $field, array('lorem','ipsum') ) == true )
       echo "$field OK\n";
    else
       echo "$field NOT OK\n";
 }

This will print: 这将打印:

lorem NOT OK
ipsum NOT OK
i.psum OK
l.o.rem OK

Regards 问候

rbo RBO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM