preg_replace( '/(?!^[<table].*?)width="\d+"/is', '', $html )

解决方案 »

  1.   

    preg_replace( '/(?![^<table].*?)width="\d+"/is', '', $html )
      

  2.   

    这是不对的,[^<table]表示不包含[^]里的任一个字符。
      

  3.   

    $html = '<table width="20"><tr><td width="10">sdfsdf</td><td   width="50"></tr></table>';
    echo preg_replace( '/(?<=\<table)\s+width="\d+"/is', '',$html);
      

  4.   

    不好意思,看错了。
    改成这样:
    $html = '<table width="20"><tr><td width="10">sdfsdf</td><td   width="50"></tr></table>';
    echo preg_replace( '/(?!(?<=\<table))\s+width="\d+"/is', '',$html);
      

  5.   

    (?<=\<img)这段表示什么意思呢,有些看不懂。
      

  6.   

    如果,在扩展些包括文本里的width="10"里的数据也要替换,例:
    <table width="100"><tr><td width="100"></td></tr></table>
    <img width="200">
    11111111111width="300"22222222222替换成:
    <table width="100"><tr><td width=""></td></tr></table>
    <img width="">
    11111111111width=""22222222222
      

  7.   

    (?<=\<img)?<=      反向预搜索,表示左边必须等于<img
      

  8.   

    $html = preg_replace( '/(?!(?<=\<table))\s+width="\d+"/is', '',$html);
    echo preg_replace( '/(?<=\S)width="\d+"/is', '',$html);
      

  9.   

    谢谢。
    \<
    <也需要转义吗?
    <img是字符串,这里的大于等于有什么意义?'/(?!<table.*?)(width="\d+")/is';
    这句为什么不行,即要把除table里的参数外所有的width=""字串都删除。
      

  10.   

    理解有误。(?!pattern)A zero-width negative lookahead assertion. For example /foo(?!bar)/ matches any occurrence of ``foo'' that isn't followed by ``bar''. Note however that lookahead and lookbehind are NOT the same thing. You cannot use this for lookbehind.
      

  11.   

    \<
    <也需要转义吗?
    (?<=\<table)
    <table是字符串,这里的大于等于有什么意义?
      

  12.   

    在?<= 里,< 需要转义,其它情况不需要转义。(?<=pattern)A zero-width positive lookbehind assertion. For example, /(?<=\t)\w+/ matches a word following a tab, without including the tab in $&. Works only for fixed-width lookbehind.
      

  13.   

    '/(?!(?<=\<table))[^<>]*?width="\d+"/is'要考虑<table xxx width="200"这种情况的话,我这个写法也不对,该怎么写?
      

  14.   

    php里正则还能正向预查呢?
    真是不错啊。
    这个是用JS。没有用预查。。JS里没正向预查。。
    <script type="text/javascript">
    var s = '<xx width="100000"><xx width=\'100000\'><wc width=100><table width="100"></table><\/xx>';
    var p = /(<table[^>]+>)|(<[^>]+?)\swidth=([\"']?)\d+\3([^>]*>)/gi;
    var ns = s.replace(p, function (a, b, c, d, e) {
    if (c) {
    return c + e;
    } else {
    return a;
    }
    });
    alert(ns);
    </script>