doubi2145 2014-12-24 16:57
浏览 48
已采纳

正则表达式除了字符串+组的使用

I'm trying to make a whitelist of html tags, here's my code :

$string = "<x>-<x>";
$result = preg_match('#^<(?!white1|white2)>.*<(\1)>$#i', $string);

But it returns false, and I don't know why. I simplified the regex to avoid confusions, but this is still the same idea.

I want to match every correct tag but the ones I want to keep safe. This regex will go on a preg_replace to erase every matched tag and let the ones I allow.

Thanks for your help in advance !

EDIT: If I find a way to do this with regexs, I'll put the solution here. But for now, I'll do it with strip_tags().

EDIT2: The easiest way I thought to is to parse all the tags and then revert back the ones we allow.

  • 写回答

1条回答 默认 最新

  • dongzhang5787 2014-12-24 17:28
    关注

    If you want to eliminate unwanted tags, you can use strip_tags:

    $allowedTags     = '<p><a><img>';
    $filteredContent = strip_tags($content, $allowedTags);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答