Php 使用preg_replace移除img类

Php 使用preg_replace移除img类,php,preg-replace,Php,Preg Replace,我想删除php中的以下行 我尝试了以下方法,但不起作用 preg_replace("@<img class=\"hSprite\".*?>@s", "", $html); 编辑 我想删除整行,这样就不会有任何输出您只需选择类。这应该可以为您做到: preg_replace('/(?<=\<img)( ?class="[\w]+")/', '', $html); 您可以使用php函数替换此类。使用preg\u replace从字符串中删除img标记 <?php

我想删除php中的以下行

我尝试了以下方法,但不起作用

preg_replace("@<img class=\"hSprite\".*?>@s", "", $html);
编辑
我想删除整行,这样就不会有任何输出

您只需选择类。这应该可以为您做到:

preg_replace('/(?<=\<img)( ?class="[\w]+")/', '', $html);

您可以使用php函数替换此类。

使用preg\u replace从字符串中删除img标记

<?php

$html='nothing<img class="hSprite" src="something" width="160" height="120"
sprite="/t/3010187.jpg" id="3010187">';

$content = preg_replace("/<img[^>]+\>/i", "", $html); 
echo $content;

为什么不在这里使用dom和xpath查询在php中尝试dom类呢?它可以从该img节点删除该类。不要依赖正则表达式来处理与dom相关的事情,请使用php团队提供的dom类

$text = 
<<<heredoc
        <img class="hSprite" src="something" width="160" height="120"
sprite="/t/3010187.jpg" id="3010187">
heredoc;

$doc = new DOMDocument();
$doc->loadHTML($text);

//$img = $doc->getElementsByTagName("img")->item(0);

$xpath = new DOMXPath($doc);

$expression = "//*[contains(@class, 'hSprite')]";
$classElements = $xpath->query($expression);

foreach ($classElements as $element) {
    //$element->attributes->getNamedItem("class")->nodeValue = '';
    $element->parentNode->removeChild($element);
}

//echo $doc->saveHTML($img);
echo $doc->saveHTML();

yuo可以使用以下简单的正则表达式:

/<img.*?class="hSprite".*?>/
i、 e:

演示:


什么不起作用?你得到了什么?你期望得到什么?我想删除所有东西你说的整行是什么意思?你的正则表达式对我来说很好。你得到了什么?您是否有任何错误消息?请仔细阅读问题,目标是删除所有标记,而不是将class属性设置为空字符串。是的,整个img节点都已更改。@CasimiretHippolyte在收到您宝贵的建议后再次更改,谢谢。还通过注释来维护旧代码。这将消除OP不想要的一切。他想删除class=hSprite的行,而不是其他行。@Toto他编辑了他的问题,他说编辑我想删除整行,所以应该没有输出整行不是所有的行!!这是可行的,但是如果有两个图像包含一个hSprite类,那么这个正则表达式将捕获其中两个图像之间的所有内容。
$text = 
<<<heredoc
        <img class="hSprite" src="something" width="160" height="120"
sprite="/t/3010187.jpg" id="3010187">
heredoc;

$doc = new DOMDocument();
$doc->loadHTML($text);

//$img = $doc->getElementsByTagName("img")->item(0);

$xpath = new DOMXPath($doc);

$expression = "//*[contains(@class, 'hSprite')]";
$classElements = $xpath->query($expression);

foreach ($classElements as $element) {
    //$element->attributes->getNamedItem("class")->nodeValue = '';
    $element->parentNode->removeChild($element);
}

//echo $doc->saveHTML($img);
echo $doc->saveHTML();
/<img.*?class="hSprite".*?>/
<?php

$html = <<< LOL
 <div class="refsect1 description" id="refsect1-reserved.variables.server-description">
  <h3 class="title">Description</h3>
  <p class="para">
   <var class="varname"><var class="varname">test</var></var> is an array containing information
   such as headers, paths, and script locations. The entries in this
   array are created by the web server. There is no guarantee that
   every web server will provide any of these; servers may omit some,
   or provide others not listed here. That said, a large number of
   these variables are accounted for in the <a href="http://www.faqs.org/rfcs/rfc3875" class="link external">test 9999</a>, so you should
   be able to expect those.
  </p>
<img class="hSprite" src="something" width="160" height="120"
sprite="/t/3010187.jpg" id="3010187">
LOL;

$newHtml = preg_replace('/<img.*?class="hSprite".*?>/sim', '', $html);

echo  $newHtml;