将数据mfp src属性添加到图像标签PHP
其内容如下:将数据mfp src属性添加到图像标签PHP,php,xpath,preg-replace,domdocument,attr,Php,Xpath,Preg Replace,Domdocument,Attr,其内容如下: <div class="image"> <img src="https://www.gravatar.com/avatar/" alt="test" width="50" height="50"> </div> 这是我的代码,它可以正常工作,但我想使用preg_replace,原因如下: function lazyload_images( $content ){ $content = mb_convert_encoding($co
<div class="image">
<img src="https://www.gravatar.com/avatar/" alt="test" width="50" height="50">
</div>
这是我的代码,它可以正常工作,但我想使用preg_replace,原因如下:
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));
}
函数lazyload_图像($content){
$content=mb_convert_编码($content,'HTML-ENTITIES',“UTF-8”);
$dom=新的DOMDocument;
libxml\u使用\u内部错误(true);
@$dom->loadHTML($content);
libxml\u使用\u内部错误(false);
$xpath=newdomxpath($dom);
foreach($xpath->evaluate('//div[img]')为$paragraphWithImage){
//$paragraphWithImage->setAttribute('class','test');
foreach($paragraphWithImage->getElementsByTagName('img')作为$image){
$image->setAttribute('data-mfp-src',$image->getAttribute('src');
$image->removeAttribute('src');
}
};
返回preg_replace('~]*>\s*~i','$dom->saveHTML($dom->documentElement));
}
作为隔离src
值并将新属性设置为该值的可靠方法,我建议您避免使用正则表达式。并不是说它不能完成,而是说如果在
中添加了更多的类,或者
HTML;
$dom=新的DOMDocument;
$dom->loadHTML($html,LIBXML\u html\u noimpled | LIBXML\u html\u NODEFDTD);
$xpath=newdomxpath($dom);
//在多次出现的情况下使用循环
foreach($xpath->query(//div[contains(@class,'image')]]/img)作为$node){
$node->setAttribute('data-mfp-src',$node->getAttribute('src');
}
echo$dom->saveHTML();
输出:
$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();
资源:
只是想让你看看正则表达式的样子 查找:
~
替换:
演示:不过,我还是不推荐它,因为它可能会在将来默默地让你失望。@mickmackusa done.我已经解决了这个问题一段时间了,但感谢你提出我的老问题xD xD,当然你今天花时间和我在一起:)好的,我只是在收拾残局。我看到了一个悬而未决的问题,想提供解决方案。记住,你完全可以自己解决问题,并对自己的问题给出答案——这有助于他人。
$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>