dqm74406 2017-12-14 08:05
浏览 107
已采纳

iframe存在问题

I have a function which compares emails contents from a few tables. If the contents differ, I want to display it for comparison. I am trying to do that using iframes and srcdoc attribute. This is a fragment of my email which has got inline styles and nested quotes.

<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">

I tried to replace all quotes with that function.

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

but it does not work. I have also tried

htmlentities($row1['email_content']) 

and

addslashes($row1['email_content']) 

but it also did not work. How can i display email content in an iframe properly?

  • 写回答

1条回答 默认 最新

  • dqd82461 2017-12-14 09:21
    关注

    I was curious about this so knocked up a couple of quick test pages to test for myself what you were saying ~ seems obvious but whatever character is used with srcdoc ( ie: srcdoc=' or srcdoc=" ) must be escaped/replaced when generating the content.

    <!-- 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 java连接sqlserver有问题
    • ¥15 yolov8 如何调cfg参数
    • ¥15 这个四人抢答器代码哪儿有问题?仿真程序怎么写?
    • ¥15 burpsuite密码爆破
    • ¥15 关于#ubuntu#的问题,如何解决?(相关搜索:移动硬盘)
    • ¥15 scikit安装之后import不了
    • ¥15 Ros2编译一个使用opencv的c++节点的时候,报了这个错误,请问怎么解决啊
    • ¥15 人脸识别相关算法 YOLO,AI等
    • ¥15 spark问题方便加V17675797079
    • ¥15 Python代码不打印的原因