我正在尝试使用preg_replace
将图像和视频(vimeo)与我的wordpress项目文章的文本内容分开。仅过滤掉了图片,但没有过滤掉iframe视频。
<div class="post_text">
<?php
$content = get_the_content();
$content = preg_replace('/<img[^>]+\>/i', '', $content);
$content = preg_replace('/<iframe[^>]+>.*?<\/iframe>/', '', $content);
$content = apply_filters('the_content', $content);
echo $content;
?>
</div>
要替换的iframe代码(无效):
<iframe src="https://player.vimeo.com/video/261467359?app_id=122963" width="500" height="281" frameborder="0" title="Mind your Step" allow="autoplay; fullscreen" allowfullscreen></iframe>
要替换的图片代码(有效):
<img src="http://localhost:8888/wp-content/uploads/2019/01/2018_Ichni_NiakaOngChrapana_4-1024x682.jpg" alt="" class="wp-image-15" srcset="http://localhost:8888/wp-content/uploads/2019/01/2018_Ichni_NiakaOngChrapana_4-1024x682.jpg 1024w, http://localhost:8888/wp-content/uploads/2019/01/2018_Ichni_NiakaOngChrapana_4-300x200.jpg 300w, http://localhost:8888/wp-content/uploads/2019/01/2018_Ichni_NiakaOngChrapana_4-768x512.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" />
我已经使用实时正则表达式网站检查了正则表达式,并且可以正常运行;当实施到wordpress代码中时,它只是不起作用。视频仍然照常显示。奇怪的是,它可以很好地过滤掉所有图像。
答案 0 :(得分:0)
只需删除定界符即可。
$content = preg_replace('<iframe[^>]+>.*?<\/iframe>', '', $content);