<iframe>
,我们需要用HTML实体替换srcdoc字符串中的引号和和符号:
我们可以使用在HTML语法中,作者只需记得使用"""(U+0022)字符包装属性内容,然后转义所有"""(U+0022)和U+0026 AMPERSAND(&)字符,并指定沙盒属性,以确保嵌入内容的安全性。
请注意引号必须被转义(否则srcdoc属性将会提前结束),并且沙盒内容中提到的原始和符号(例如在URL或散文中)必须双重转义——一次是为了在最初解析srcdoc属性时保留和符号,再一次是为了防止在解析沙盒内容时误解和符号。
str_replace()
在PHP中实现这一点:$srcdoc = '<div id="foo">"Contains quoted text."</div>';
$escaped = str_replace([ '"', '&' ], [ '"', '&amp;' ], $srcdoc);
<div id="foo">"Contains quoted text."</div>
srcdoc
属性的值:
<div id="email_content">
<iframe sandbox srcdoc="<div id="foo">"Contains quoted text."</div>"></iframe>
</div>
src
和href
属性内部特别让人恼火,因为它会破坏路径。<script src='path.js'>
。 - ado387您需要在php中使用htmlentities
方法。
<?php
$str = '<a href="https://www.tarunlalwani.com">Go to tarunlalwani.com</a>';
echo htmlentities($str);
?>
srcdoc
。它的效果如下所示JSFiddle链接:https://jsfiddle.net/zpx8qw1b/1/
htmlentities
!它速度较慢,输出结果会更大,因为你不需要替换每个特殊字符以用于 srcdoc,只需替换引号即可。另一个使用str_replace
的答案效率要高得多。 - mr_carrerahtmlentities()
也不能解决在srcdoc字符串中双重转义和号(&)的需求。 - Cy Rossignol