我有一个比较几张桌子的电子邮件内容的功能。
如果内容不同,我想显示它以进行比较。我试图使用iframes
和srcdoc
属性来做到这一点。这是我的电子邮件的一个片段,它有内联样式和嵌套引号。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<title>Title</title>
</head>
<style type="text/css">a:visited {color: #fff;}</style>
<body style="background: #fff; margin-top:25px; margin-bottom:30px; padding-top:0; padding-bottom:0;">
<table align="center">
我尝试用该函数替换所有引号。
str_replace([ '"', '&' ], [ '"', '&amp;' ],$row1['email_content'])
但它不起作用。我也试过
htmlentities($row1['email_content'])
和
addslashes($row1['email_content'])
但它也没有用。如何正确显示iframe
中的电子邮件内容?
答案 0 :(得分:1)
我很好奇这个,所以敲了几个快速测试页来测试你自己说的是什么〜似乎很明显但是srcdoc使用的是什么字符(即:srcdoc='
或srcdoc="
)生成内容时必须进行转义/替换。
<!-- mickeymouse.html ~ used as source for `srcdoc` -->
<html>
<head>
<title>Mickey Mouse loved Minnie</title>
</head>
<body>
<h1>Mickey Mouse</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam non finibus nisl. Etiam ut velit ut est placerat dictum. </p>
<!-- content populated by inline javascript within iframe srcdoc html -->
<div id="donaldduck">nothing to see here</div>
<script>document.getElementById("donaldduck").innerHTML="poor wiley coyote, when will he catch that damn bird?";</script>
<!-- The line below caused the iframe to not correctly render before doing str_replace to edit the single quotes -->
<p>If this text has a single quote - like ' it will cause whatever follows to not render and breaks the `srcdoc` content</p>
</body>
</html>
<!-- iframe page - will display mickeymouse.html -->
<html>
<head>
<title>iframe - srcdoc</title>
</head>
<body>
<?php
$file='mickeymouse.html';
$html=file_get_contents( $file );
/*
' -> '
" -> "
*/
?>
<iframe srcdoc="<?php echo str_replace( '"', '"', $html ); ?>" width=800 height=600 sandbox='allow-forms allow-scripts allow-same-origin'></iframe>
</body>
</html>