Mic*_*l S 0 php string iframe find
在我的页面上,用户可以在其音乐中插入嵌入链接.但是如果对象的宽度超过600px,则它不适合页面.所以我正在寻找一些方法来找到iframe标签的width参数,如果值大于600px,则将其更改为600px.但我的谷歌技能让我失望,所以我希望这里有人可以提供帮助.插入的iframe看起来像这样:
<iframe width="660" height="180" src="//www.mixcloud.com/widget/iframe/?feed=http%3A%2F%2Fwww.mixcloud.com%2FCLCK_Podcast%2Fclck-podcast-20-czscream%2F&embed_type=widget_standard&embed_uuid=fd5ce7dc-9687-45e1-8368-27631b48385f&hide_tracklist=1&replace=0&hide_cover=1" frameborder="0"></iframe>
Run Code Online (Sandbox Code Playgroud)
以下是如何使用PHP的本机DOM扩展正确完成它.与正则表达式不同,这保证适用于所有情况,并且在提供稍微不同的HTML标记格式时不会阻塞:
$dom = new DOMDocument;
$dom->loadHTML($html);
$html = '';
foreach ($dom->getElementsByTagName('iframe') as $iframe) {
// get the 'width' attribute value
$width = (int) $iframe->getAttribute('width');
// obvious
if ($width > 600) {
$iframe->setAttribute('width', 600);
}
// append the modified HTML to string
$html .= $dom->saveHTML($iframe) . "\n";
}
echo $html;
Run Code Online (Sandbox Code Playgroud)
这将检查所有标记的width属性,并在当前值大于该值时将其设置为. <iframe>600