我让编辑以这种格式给我gettyimages的嵌入代码:
<div class="getty embed image" style="background-color:#fff;display:inline-block;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;color:#a7a7a7;font-size:11px;width:100%;max-width:594px;">
<div style="padding:0;margin:0;text-align:left;"><a href="http://www.gettyimages.com/detail/473144498" target="_blank" style="color:#a7a7a7;text-decoration:none;font-weight:normal !important;border:none;display:inline-block;">Embed from Getty Images</a></div>
<div style="overflow:hidden;position:relative;height:0;padding:65.656566% 0 0 0;width:100%;">
<iframe src="//embed.gettyimages.com/embed/473144498?et=jFJ38un7Qy1YOLsguPNmmA&viewMoreLink=on&sig=YGqYtdBCwZUYgO864KJJ6ulXuBuS1glNjjOGOHCJ28M=&caption=true" width="594" height="390" scrolling="no" frameborder="0" style="display:inline-block;position:absolute;top:0;left:0;width:100%;height:100%;margin:0;"></iframe>
</div><p style="margin:0;"></p></div>
我无法使用Gettyimages中的所有额外div标签来操纵图像大小,并尝试将其替换为以下格式:
<iframe src="//embed.gettyimages.com/embed/473144498?et=jFJ38un7Qy1YOLsguPNmmA&viewMoreLink=on&sig=YGqYtdBCwZUYgO864KJJ6ulXuBuS1glNjjOGOHCJ28M=&caption=true" width="594" height="390"></iframe>
但到目前为止我没有运气。任何人都可以帮助我吗?
答案 0 :(得分:1)
我为此使用了domdocument解析器,而不是正则表达式。
<?php
$string = '<div class="getty embed image" style="background-color:#fff;display:inline-block;font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;color:#a7a7a7;font-size:11px;width:100%;max-width:594px;">
<div style="padding:0;margin:0;text-align:left;"><a href="http://www.gettyimages.com/detail/473144498" target="_blank" style="color:#a7a7a7;text-decoration:none;font-weight:normal !important;border:none;display:inline-block;">Embed from Getty Images</a></div>
<div style="overflow:hidden;position:relative;height:0;padding:65.656566% 0 0 0;width:100%;">
<iframe src="//embed.gettyimages.com/embed/473144498?et=jFJ38un7Qy1YOLsguPNmmA&viewMoreLink=on&sig=YGqYtdBCwZUYgO864KJJ6ulXuBuS1glNjjOGOHCJ28M=&caption=true" width="594" height="390" scrolling="no" frameborder="0" style="display:inline-block;position:absolute;top:0;left:0;width:100%;height:100%;margin:0;"></iframe>
</div><p style="margin:0;"></p></div>';
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($string);
libxml_clear_errors();
$doc->getElementsByTagName('iframe')->item(0)->removeAttribute('style');
echo $doc->saveHTML();
输出:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><div class="getty embed image" style="background-color:#fff;display:inline-block;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;color:#a7a7a7;font-size:11px;width:100%;max-width:594px;">
<div style="padding:0;margin:0;text-align:left;"><a href="http://www.gettyimages.com/detail/473144498" target="_blank" style="color:#a7a7a7;text-decoration:none;font-weight:normal !important;border:none;display:inline-block;">Embed from Getty Images</a></div>
<div style="overflow:hidden;position:relative;height:0;padding:65.656566% 0 0 0;width:100%;">
<iframe src="//embed.gettyimages.com/embed/473144498?et=jFJ38un7Qy1YOLsguPNmmA&viewMoreLink=on&sig=YGqYtdBCwZUYgO864KJJ6ulXuBuS1glNjjOGOHCJ28M=&caption=true" width="594" height="390" scrolling="no" frameborder="0"></iframe>
</div><p style="margin:0;"></p></div></body></html>
这假设您只有1 iframe
。如果你有多次分配
$doc->getElementsByTagName('iframe')
到一个变量,然后遍历它。
如果您只想要height
,width
和src
属性,最好选择那些然后构建元素。否则,您将必须删除用户可以添加的每个属性..
所以修改上述方法:
$iframes = $doc->getElementsByTagName('iframe');
foreach($iframes as $iframe) {
$src = $iframe->getAttribute('src');
$height = $iframe->getAttribute('height');
$width = $iframe->getAttribute('width');
echo "<iframe src='$src' height='$height' width='$width'></iframe>";
}
这会给:
<iframe src='//embed.gettyimages.com/embed/473144498?et=jFJ38un7Qy1YOLsguPNmmA&viewMoreLink=on&sig=YGqYtdBCwZUYgO864KJJ6ulXuBuS1glNjjOGOHCJ28M=&caption=true' height='390' width='594'></iframe>
你也可以使用"
进行属性封装,你只需要将它们转义,或者连接变量并使用单引号进行封装。
答案 1 :(得分:0)
通过#(( [a-z]+)="([^"])+")+#
perg_match_all