将 data-mfp-src 属性添加到图像标签 PHP
Add data-mfp-src attribute to image tags PHP
他的内容是:
<div class="image">
<img src="https://www.gravatar.com/avatar/" alt="test" width="50" height="50">
</div>
我想使用preg_replace
添加data-mfp-src
属性(从src
属性获取值)作为最终代码如下:
<div class="image">
<img src="https://www.gravatar.com/avatar/" data-mfp-src="https://www.gravatar.com/avatar/" alt="test" width="50" height="50">
</div>
这是我的代码,它没有任何问题,但我想使用 preg_replcae 出于某些特定原因:
function lazyload_images( $content ){
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
$dom = new DOMDocument;
libxml_use_internal_errors(true);
@$dom->loadHTML($content);
libxml_use_internal_errors(false);
$xpath = new DOMXPath($dom);
foreach ($xpath->evaluate('//div[img]') as $paragraphWithImage) {
//$paragraphWithImage->setAttribute('class', 'test');
foreach ($paragraphWithImage->getElementsByTagName('img') as $image) {
$image->setAttribute('data-mfp-src', $image->getAttribute('src'));
$image->removeAttribute('src');
}
};
return preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', $dom->saveHTML($dom->documentElement));
}
作为隔离 src
值并将新属性设置为此值的可靠方法,我建议您避免使用正则表达式。并不是说它不能完成,而是如果将更多 类 添加到 <div>
或者 <img>
属性被移动,我接下来的代码片段不会中断。
代码:(Demo)
$html = <<<HTML
<div class="image">
<img src="https://www.gravatar.com/avatar/" alt="test" width="50" height="50">
</div>
HTML;
$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
// using a loop in case there are multiple occurrences
foreach ($xpath->query("//div[contains(@class, 'image')]/img") as $node) {
$node->setAttribute('data-mfp-src', $node->getAttribute('src'));
}
echo $dom->saveHTML();
输出:
<div class="image">
<img src="https://www.gravatar.com/avatar/" alt="test" width="50" height="50" data-mfp-src="https://www.gravatar.com/avatar/">
</div>
资源:
- http://php.net/manual/en/domelement.setattribute.php
- http://php.net/manual/en/domelement.getattribute.php
只是为了向您展示正则表达式的样子...
查找:~<img src="([^"]*)"~
替换:<img src="" data-mfp-src=""
演示:https://regex101.com/r/lXIoFw/1但是,我还是不推荐它,因为它可能会在未来默默地让你失望。
他的内容是:
<div class="image">
<img src="https://www.gravatar.com/avatar/" alt="test" width="50" height="50">
</div>
我想使用preg_replace
添加data-mfp-src
属性(从src
属性获取值)作为最终代码如下:
<div class="image">
<img src="https://www.gravatar.com/avatar/" data-mfp-src="https://www.gravatar.com/avatar/" alt="test" width="50" height="50">
</div>
这是我的代码,它没有任何问题,但我想使用 preg_replcae 出于某些特定原因:
function lazyload_images( $content ){
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
$dom = new DOMDocument;
libxml_use_internal_errors(true);
@$dom->loadHTML($content);
libxml_use_internal_errors(false);
$xpath = new DOMXPath($dom);
foreach ($xpath->evaluate('//div[img]') as $paragraphWithImage) {
//$paragraphWithImage->setAttribute('class', 'test');
foreach ($paragraphWithImage->getElementsByTagName('img') as $image) {
$image->setAttribute('data-mfp-src', $image->getAttribute('src'));
$image->removeAttribute('src');
}
};
return preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', $dom->saveHTML($dom->documentElement));
}
作为隔离 src
值并将新属性设置为此值的可靠方法,我建议您避免使用正则表达式。并不是说它不能完成,而是如果将更多 类 添加到 <div>
或者 <img>
属性被移动,我接下来的代码片段不会中断。
代码:(Demo)
$html = <<<HTML
<div class="image">
<img src="https://www.gravatar.com/avatar/" alt="test" width="50" height="50">
</div>
HTML;
$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
// using a loop in case there are multiple occurrences
foreach ($xpath->query("//div[contains(@class, 'image')]/img") as $node) {
$node->setAttribute('data-mfp-src', $node->getAttribute('src'));
}
echo $dom->saveHTML();
输出:
<div class="image">
<img src="https://www.gravatar.com/avatar/" alt="test" width="50" height="50" data-mfp-src="https://www.gravatar.com/avatar/">
</div>
资源:
- http://php.net/manual/en/domelement.setattribute.php
- http://php.net/manual/en/domelement.getattribute.php
只是为了向您展示正则表达式的样子...
查找:~<img src="([^"]*)"~
替换:<img src="" data-mfp-src=""
演示:https://regex101.com/r/lXIoFw/1但是,我还是不推荐它,因为它可能会在未来默默地让你失望。