如果alt标签不为空,请替换

时间:2018-09-09 12:23:40

标签: php regex replace 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">

虽然我希望输出为<img src="test2.jpg" alt="HELLO">

编辑:我之前尝试使用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');

问题很少。由于某种原因,它正在修改帖子内容。 <p>标记已删除,并替换为<br>。我通过使用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 -->

有人可以帮我吗?谢谢

1 个答案:

答案 0 :(得分:1)

似乎最好的解决方法是

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

详细信息

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