duanhuan7750 2011-03-24 06:51
浏览 20
已采纳

PHP - 从特定网站查找链接并删除

I got a string which has a lot of bad links from bad website and there are also useful links for my user.

I need a way to remove the bad links only. such as method below

remove('regex', badsite.com, ''); // remove all links from badsite.com
remove('regex', viagra.com, ''); // remove links from viagra.com
  • 写回答

2条回答 默认 最新

  • doob0526 2011-03-24 07:08
    关注

    Check this I think this may help you...

    <?php
    function remove($string) {
      $patterns = array();
      $patterns[0] = '/http:\/\/www.badsite.com/';
      $patterns[1] = '/http:\/\/www.viagra.com/';
      $patterns[2] = '/badsite.com/';
      $patterns[3] = '/viagra.com/';
    
    
    
      $replacements = array();
      $replacements[0] = '**Remove**';
      $replacements[1] = '**Remove**';
      $replacements[2] = '**Remove**';
      $replacements[3] = '**Remove**';
    
      echo preg_replace($patterns, $replacements, $string);
    }
    
    $string = "This is my url http://www.badsite.com and one more dirty site http://www.viagra.com one good site http://www.google.com";
    
    remove($string);
    ?>
    

    Output -

    This is my url Remove and one more dirty site Remove one good site http://www.google.com

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面