iframe存在问题

时间:2017-12-14 08:05:02

标签: php html5 iframe

我有一个比较几张桌子的电子邮件内容的功能。 如果内容不同,我想显示它以进行比较。我试图使用iframessrcdoc属性来做到这一点。这是我的电子邮件的一个片段,它有内联样式和嵌套引号。

<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;">&nbsp;
        <table align="center">

我尝试用该函数替换所有引号。

str_replace([ '"', '&' ], [ '&quot;', '&amp;amp;' ],$row1['email_content'])

但它不起作用。我也试过

htmlentities($row1['email_content']) 

addslashes($row1['email_content']) 

但它也没有用。如何正确显示iframe中的电子邮件内容?

1 个答案:

答案 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 );
            /*

                '   ->  &#39;
                "   ->  &#34;

            */

        ?>
        <iframe srcdoc="<?php echo str_replace( '"', '&#34', $html ); ?>" width=800 height=600 sandbox='allow-forms allow-scripts allow-same-origin'></iframe>
    </body>
</html>