regex - PHP preg_replace() 模式查找具有特定文件夹的 img
问题描述
我正在尝试用占位符替换博客文章 the_content() 中的损坏图像。我的媒体文件夹中的两个文件夹丢失,这导致图像损坏。
所以以下建议对我有用,我觉得
<?php
$content = get_the_content();
$content = preg_replace("/<img[^>]+\>/i", "placeholderim", $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content); echo $content; ?>
但我想将模式与图像匹配,图像 URL 应包含 2017 || 2018. 如何创建这种模式。请提供任何帮助!
解决方案
这会查找所有包含 2019 或 2020 的 html 图像标签。
/(<img[^>]+(2019|2020)+[^>]+>)/ig
演示:https ://regex101.com/r/0rdwd2/1
对于 PHP(未经测试),这将是:
$content = get_the_content();
$content = preg_replace("/(<img[^>]+(2019|2020)+[^>]+>)/ig", "placeholderim", $content);
推荐阅读
- r - 根据文件扩展名选择文件的最佳方法(用 r 编写)
- bash - 禁止 stderr 控制台输出,但在 Shell 中保留退出代码
- python - 压缩 Python 生成器,第二个更短:如何检索被静默消耗的元素
- reactjs - 在将数据存储到firebase并在react redux中更新状态时出现以下错误
- laravel - 现有列的 renameColumn 迁移失败,列不存在错误
- java - 图传递性 Java
- android - Facebook 应用程序中的深层链接有什么问题?
- java - 在较大的文件上获取 java.lang.OutOfMemoryError
- tensorflow - 在 Ubuntu 和 OSX 上构建 tensorflow 需要 9 个多小时并且尚未完成
- google-apps-script - 有没有办法选择 MimeType 的多个或所有属性?