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