【发布时间】:2015-05-25 14:54:07
【问题描述】:
这是来自 WordPress 插件的一段 PHP 代码,它使用 preg_replace 将图像添加到内容中。问题是添加的图像没有指定尺寸,并且由于在每个页面上多次添加此图像,它会减慢页面渲染速度(在很小程度上)。现在,问题是:
如何在通过下面提到的 PHP 正则表达式添加的图像的最终 HTML 中添加宽度 = "1" 和高度 = "1"?
// In case you want to change the placeholder image
$placeholder_image = apply_filters( 'lazyload_images_placeholder_image', self::get_url( 'images/1x1.trans.gif' ) );
// This is a pretty simple regex, but it works
$content = preg_replace( '#<img([^>]+?)src=[\'"]?([^\'"\s>]+)[\'"]?([^>]*)>#', sprintf( '<img${1}src="%s" data-lazy-src="${2}"${3}><noscript><img${1}src="${2}"${3}></noscript>', $placeholder_image ), $content );
return $content;
任何帮助将不胜感激,谢谢!
【问题讨论】: