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 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳