dqhdz04240 2015-03-23 03:48 采纳率: 0%
浏览 19

正则表达式检查垃圾邮件发送者

I'm trying to figure out the regular expression for preg_match() that would return true for all of the following scenarios:

1- strip spaces for people doing this "check out my website . com (no spaces)"

2- find the obvious "mywebsite.com" in "check out mywebsite.com"

3- find dot replacement "check out mywebsite dot com (no spaces)"

4- be case insensitive

  • 写回答

1条回答 默认 最新

  • dongzhe3171 2015-03-23 04:34
    关注

    Does this work for you? I think you'll have to modify this in the future but for a rough implementation (what you've asked for) it should work. I also presumed your "check out my" is required text if not that can be removed. The \s* means any amount of whitespace; the \w* means any number of word characters (a-z, 0-9, and/or underscore). The | means "or" and the () groups the two values that "or" should be affecting. If you have questions please ask. The outer most parenthesis group the potential domain. You may want to alter the \w* to \w+ but I'd presume if they're saying "check out my .com" you'd also want to filter it...

    if(preg_match('~check\s*out\s*my\s*(\w*\s*(\.|dot)\s*com)~i', $input, $domain)) {
         echo 'This is spam?';
    }
    

    A solution based on your actual code:

    <?php
    $spamCount=0;
    $input=preg_replace(array('~\s*~', '~dot~i'), array('', '.'), 'Hi. Please see my website at "test domain . com" (no spaces) anothertest dot com');
    $checkTheseDomains = array("testdomain.com","anothertest.com");
    foreach($checkTheseDomains as $domain) {
        if(strpos($input, $domain) !== false) {
            $spamCount++;
        }
    }
    if($spamCount > 0){
            echo "Spam count of  <b>".$spamCount."</b>";
    }else{
            echo "No spam";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭