douyan2970 2013-06-17 08:37
浏览 28
已采纳

在PHP中生成包含HTML的XML - 变量问题[关闭]

I am sitting on a little problem here:

I have a php file which generates xml data.

$requestXmlBody .= "<Version>$compatabilityLevel</Version>";

Now there are variables pulled from the upper php code and also HTML is generated

$requestXmlBody .= 
    '<Description>
        <![CDATA[
            <!DOCTYPE html>
            <html>
                <head>
                </head>
                <body>
                    <script type="text/javascript">
                        <!--some JS-->
                    </script>
                    <img src="http://www.myserver.com/pic.jpg" class="etalage_thumb_image" />
                </body>
            </html>
        ]]>
    </Description>';

Now strangely I cannot mix variables and HTML Code. As you can see I use CDATA for the HTML. I want to use a variable for the image name rather than a fixed link. So the code would look like this

$requestXmlBody .= '<Description>
    <![CDATA[
        <!DOCTYPE html>
        <html>
            <head>
            </head>
            <body>
                <script type="text/javascript">
                    <!--some JS-->
                </script>
                <img src="$imagelink" class="etalage_thumb_image" />
            </body>
        </html>
    ]]>
</Description>';

But this just does not work. I tried this

$requestXmlBody .= '<Description>
    <![CDATA[
        <!DOCTYPE html>
        <html>
            <head>
            </head>
            <body>
                <script type="text/javascript">
                    <!--some JS-->
                </script>
                <img src="]]>$imagelink<![CDATA[" class="etalage_thumb_image" />
            </body>
        </html>
]]>
</Description>';

But also this will not work. I even tried to hand over the php variable (which I grab from a session btw) to a JS variable and include it with document.write

Still no success.

This one would work

$requestXmlBody .= '<Description>
    $imagelink
</Description>';

But not together with the generated HTML code as you can see above.

Any help is appreciated.

Thanks

  • 写回答

2条回答 默认 最新

  • dqb78642 2013-06-17 11:33
    关注

    Separate concerns. Don't do several things at once. If you split your embedded HTML into its own variable, it gets much easier.

    As soon as you have 'freed' the HTML string from the XML context, you'll see that the problem still exists. It is caused by quoting the string with single quotes, which prevent interpolation. You have to use string concatenation instead of embedding the variable directly.

    $description = '<!DOCTYPE html>
        <html>
            <head>
            </head>
            <body>
                <script type="text/javascript">
                    <!--some JS-->
                </script>
                <img src="' . $imagelink . '" class="etalage_thumb_image" />
            </body>
        </html>';
    
    $requestXmlBody .= '<Description>
        <![CDATA[' . $description . ']]>
    </Description>';
    

    Be sure that your HTML string does not contain a CDATA section itself, since CDATA sections cannot be nested.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动