Preg_replace img没有id

时间:2013-12-25 01:50:22

标签: php html regex image preg-replace

我无法弄清楚如何使用preg_replace删除所有img标记,除了那些具有id字段的标记,例如

<img class="wpcf7-form-control wpcf7-captchac wpcf7-captcha-captcha-489" id="captcha-489-img" width="72" height="24" alt="captcha" src="http://mysolarpower.co.nz/wp-content/uploads/wpcf7_captcha/1081225180.png" />

请勿替换此

<img class="size-large wp-image-54 alignleft" style="width: 487px; height: 595px;" title="" alt="german solar pics copy" src="/wp-content/uploads/2013/11/_d_improd_/german-solar-pics-copy-1024x955_f_improf_562x595.png" width="562" height="595" data-mce-width="487" data-mce-height="595" />

替换此

我有这个正则表达式preg_replace删除所有img标签:

$contentOut = preg_replace('/<img[^>]*>/', '', $content);

我只是想弄清楚如何排除任何有id字段的东西。

由于

3 个答案:

答案 0 :(得分:3)

如果必须为作业使用正则表达式,则可以使用否定前瞻执行此操作。

$content = preg_replace('/<img(?![^>]*id="[^"]*")[^>]*>/i', '', $content);

请参阅Live demo

考虑将来使用DOM来完成这样的工作。

$content = <<<DATA
<img class="wpcf7-form-control wpcf7-captchac wpcf7-captcha-captcha-489" id="captcha-489-img" width="72" height="24" alt="captcha" src="http://mysolarpower.co.nz/wp-content/uploads/wpcf7_captcha/1081225180.png" />
<img class="size-large wp-image-54 alignleft" style="width: 487px; height: 595px;" title="" alt="german solar pics copy" src="/wp-content/uploads/2013/11/_d_improd_/german-solar-pics-copy-1024x955_f_improf_562x595.png" width="562" height="595" data-mce-width="487" data-mce-height="595">
<img src="http://foo.jpg" id>
DATA;

$doc = new DOMDocument();
$doc->loadHTML($content); // Load your HTML content

$xpath = new DOMXPath($doc);
$imgs = $xpath->query("//img[not(contains(@id, 'captcha'))]");

foreach ($imgs as $img) {
   $img->parentNode->removeChild($img);
}

echo $doc->saveHTML();

输出(它会将img不包含id的所有captcha标记替换为<img class="wpcf7-form-control wpcf7-captchac wpcf7-captcha-captcha-489" id="captcha-489-img" width="72" height="24" alt="captcha" src="http://mysolarpower.co.nz/wp-content/uploads/wpcf7_captcha/1081225180.png"> 文字)

{{1}}

请参阅Working demo

答案 1 :(得分:1)

您可以使用这样的负面预测:

/<img(?!.*?\sid="[^"]+")[^>]*>/

所以你的代码将成为:

$contentOut = preg_replace('/<img(?!.*?\sid="[^"]+")[^>]*>/','',$content);

说明:

  <img                     '<img'
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
--------------------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
    id="                     'id="'
--------------------------------------------------------------------------------
    [^"]+                    any character except: '"' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    "                        '"'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [^>]*                    any character except: '>' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  >                        '>'

Live demo

答案 2 :(得分:-1)

[^id]<img [^>]*>在我的测试中为我工作 - 感谢RegexPal