【问题标题】:regular expression for finding out 'anything <script>anything</script> anything' anywhere in string用于在字符串中的任何位置查找“任何东西 <script>anything</script> 任何东西”的正则表达式
【发布时间】:2017-09-13 09:37:54
【问题描述】:

我正在尝试在我的 php 字符串中找出 open() 和 close() 的脚本标记。请在下面找到我的代码

public function custom_xss_clean($str){
  if(!preg_match('/(<\/?)(script|html)(>*)/i', $str)){
       return true;
  } else {
    $this->form_validation->set_message('custom_xss_clean','The %s field invalid');
    return false;
  }
}

此代码在在线工具中运行良好,但在我的字符串代码中却不行

&lt;script&gt;alert(10)&lt;/script&gt;

我尝试了这么多模式但没有运气。请帮助提前谢谢。

【问题讨论】:

  • 1.使用正则表达式解析 html 通常不是一个好主意,并且 2. 您也可以在 html 属性中使用 javascript,所以这有点毫无意义/还不够。
  • 感谢 Sahil 的快速回复,但我这样做是为了验证 codeigniter。我创建了一个回调 custom_xss_clean()。因为 CI 不再支持 XSS_CLEAN。

标签: php regex codeigniter validation server-side-validation


【解决方案1】:

试试这个

preg_match('/<script>[a-zA-Z0-9 \. \n \t\r \* \\ \* ~`!@#$%^&*()-_+={}\[\]\'":;?\/><.,|]*<\/script>/', $str);

经过测试,您也可以实时查看https://regex101.com/r/ih2C3F/1/

【讨论】:

    【解决方案2】:

    你的意思是你想得到脚本标签之间的一切?

    <?php
        preg_match_all("|<[^>]+>(.*)</[^>]+>|U","<script>example: </script><div align=left>this is a test</div>",$out, PREG_PATTERN_ORDER);
        echo '<pre>',
        print_r($out);
    

    如果您在 codeigniter 中寻找 xss_clean,请点击此链接 click

    【讨论】:

      猜你喜欢
      • 2020-10-13
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 2013-08-17
      • 1970-01-01
      相关资源
      最近更新 更多