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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。