【问题标题】:php alternative to strip_tags that allows <, <=, >=, > in sql - regex?php 替代 strip_tags 允许 <,<=,>=,> 在 sql - 正则表达式?
【发布时间】:2016-03-07 11:42:57
【问题描述】:

我在允许使用 apply_filters 函数的非 Wordpress 网站上使用 wp-db.php。我最初是这样做的:

function apply_filters($type, $input) {
    return strip_tags($input);
}

我想阻止保存 html。我以为我解决了问题,但后来注意到包含

我想要它做的是去除以字符开头的标签,例如&lt;b... 但如果括号后有空格或等号,例如&lt;&lt;= 那么它不应该删除它。

我找到了这段代码,但它并没有按照我想要的方式工作:

preg_replace('/<[^>]*>/', '', $input);

例如

<b>test</b> abc <= def < ok? ilj >= xyz >

返回为:

test abc = xyz >

它应该只删除 &lt;x...&gt; 其中 x 不是空格或等号并删除 &lt;/....&gt;

顺便说一句,我注意到 不起作用,所以我认为

【问题讨论】:

    标签: php sql regex preg-replace strip-tags


    【解决方案1】:

    只有第一个标签有效:

    <b>1</b> <0b>2</0b> < b>3</ > <'b>4</'b> <(>5</(> <=>6</=>
    

    这个非贪婪的正则表达式删除有效和结束标签:

    /&lt;[^(=\d' )].*?&gt;/g

    在 php 中:

    preg_replace('/&lt;[^(=\d\' )].*?&gt;/', '', $input);

    https://regex101.com/

    由于 sql,我有其他标签,例如:

    col1 < 10
    col2 <10
    col3 <'2010-10-10'
    col4 <(SELECT col5...)
    col5 <=20
    

    【讨论】:

      【解决方案2】:

      您可以使用一些 preg_match 字符串来执行操作。但不确定它是否适用于所有自定义字符串。所以你可以使用htmlspecialchars 函数来改变

                 &gt; is >
      
                  &lt; is <
      

      您可以在此处了解有关该功能的更多信息

      http://www.w3schools.com/html/html_entities.asp

      之后应用strip_tags,它就会为你工作。

      所以你必须使用 preg_match 函数。试试这个。

           preg_replace("# <(?![/a-z]) | (?<=\s)>(?![a-z]) #exi", "htmlentities('$0')", $html);
      

      【讨论】:

      • sql呢?它期待 而不是 <和>我还想删除所有 html,例如
      • 它没有删除我的示例中的粗体标记,我不确定它是否将
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2012-08-02
      相关资源
      最近更新 更多