【问题标题】:Replace alt tag if its not empty如果 alt 标签不为空,则替换它
【发布时间】:2019-02-14 02:30:09
【问题描述】:

我有一个正则表达式条件,如果它们是空的,它会替换图像 alt 标签。

// <img src="test1.jpg" alt="">

$replacement = '$1HELLO$2';
$pattern ='~(<img.*? alt=")("[^>]*>)~i';
$content = preg_replace($pattern, $replacement, $content);

// output <img src="test1.jpg" alt="HELLO">

我正在尝试找到一种方法,如果 alt 标签不为空,那么它应该替换整个字符串。我已经尝试过了,但它会在开头添加单词而不是替换。

// <img src="test2.jpg" alt="my alternative text">

$replacement = '$1HELLO$2';
$pattern ='~(<img.*? alt=")(.+/S.+>)~i';
$content = preg_replace($pattern, $replacement, $content);

// output <img src="test2.jpg" alt="HELLOmy alternative text">

虽然我希望输出为&lt;img src="test2.jpg" alt="HELLO"&gt;

编辑:我之前尝试过使用 DOM Parser 方法,但问题很少。这是代码。

function replaceALT($content) {
    global $post;
    $post = get_post($post->ID);
    $content = $post->post_content;
    $alt_keyword = "HELLO";
    $dom = new DOMDocument();
    $dom->loadHTML($content);

    $images = $dom->getElementsByTagName('img');

    foreach ( $images as $image) {
        if (empty($image->getAttribute("alt"))) {

            $image->setAttribute('alt', $alt_keyword);

        }
    }

    $content = $dom->saveHTML();

    return $content;
}
add_filter('the_content', 'replaceALT');

几乎没有问题。出于某种原因,它正在修改帖子内容。 &lt;p&gt; 标签已被删除并替换为 &lt;br&gt;。我通过使用return wpautop( $content ); 解决了这个问题。另一个问题是 img 自定义数据已被删除。例如,WordPress TwentySeventeen 主题在帖子中返回这样的图像。

<img src="http://localhost/wp/wp-content/uploads/2018/08/image-1356510220.jpg" alt="" width="3264" height="2448" class="alignleft size-full wp-image-24" srcset="http://localhost/wp/wp-content/uploads/2018/08/image-1356510220.jpg 3264w, http://localhost/wp/wp-content/uploads/2018/08/image-1356510220-300x225.jpg 300w, http://localhost/wp/wp-content/uploads/2018/08/image-1356510220-768x576.jpg 768w, http://localhost/wp/wp-content/uploads/2018/08/image-1356510220-1024x768.jpg 1024w" sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" />

但是 DOM 解析器返回图像是这样的。

<img src="http://localhost/wp/wp-content/uploads/2018/08/image-1356510220.jpg" alt="HELLO" width="3264" height="2448" class="alignleft size-full wp-image-24">

因为我需要替换帖子内容 div 中的 alt 标签。

<!-- default output -->
<div class="entry-content">
    <p><img src="http://localhost/wp/wp-content/uploads/2018/08/image-1356510220.jpg" alt="" width="3264" height="2448" class="alignleft size-full wp-image-24" srcset="http://localhost/wp/wp-content/uploads/2018/08/image-1356510220.jpg 3264w, http://localhost/wp/wp-content/uploads/2018/08/image-1356510220-300x225.jpg 300w, http://localhost/wp/wp-content/uploads/2018/08/image-1356510220-768x576.jpg 768w, http://localhost/wp/wp-content/uploads/2018/08/image-1356510220-1024x768.jpg 1024w" sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" />Lorem ipsum dolor sit amet</p>
</div><!-- .entry-content -->

它正在返回这样的输出。

<!-- DOM parser output -->
<div class="entry-content">
<p><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><br />
<html><body><img src="http://localhost/wp/wp-content/uploads/2018/08/image-1356510220.jpg" alt="HELLO" width="3264" height="2448" class="alignleft size-full wp-image-24">Lorem ipsum dolor sit amet</body></html></p>
</div><!-- .entry-content -->

有人可以帮我解决这个问题吗?谢谢

【问题讨论】:

  • 无论如何,基于 DOM 的解决方案会更清晰,但您可以在原始模式中将 )( 替换为 )[^"]*(
  • 非常感谢。由于我需要将这两个条件分开,所以我在第二个示例中使用了它。 $pattern2 ='~(&lt;img.*? alt=")[^"]*(.+/S.+&gt;)~i'; 它有效。您认为它好还是可以改进以找到现有的alt标签并替换它。
  • 你见过$dom = new DOMDocument('1.0', 'UTF-8'); $dom-&gt;loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);吗?
  • 完成...我刚刚加了票。

标签: php regex replace alt


【解决方案1】:

看来这里最好的解决方案是

'~(<img\s(?:[^<]*?\s)?alt=")[^"]+("[^<]*?>)~i'

详情

  • (&lt;img\s(?:[^&lt;]*?\s)?alt=") - 第 1 组:
    • &lt;img - 文字子字符串
    • \s - 一个空格
    • (?:[^&lt;]*?\s)? - 除了&lt; 之外的 0+ 个字符的可选子字符串,后跟空格
    • alt=" - 文字子串
  • [^"]+ - 1 个或多个除 " 以外的字符
  • ("[^&lt;]*?&gt;) - 第 2 组:
    • " - 一个"
    • [^&lt;]*? - 除&lt; 之外的任何 0+ 字符尽可能少
    • &gt; - &gt; 字符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    相关资源
    最近更新 更多