开发者

How would you do a code-escape in PHP?

开发者 https://www.devze.com 2023-03-21 02:08 出处:网络
I have a huge list of stuff for a glossary ( about 17 pages worth ) that I have to put into an X开发者_Python百科ML file. So I decided I\'d use php to make it. My code works, except where ALL the XML

I have a huge list of stuff for a glossary ( about 17 pages worth ) that I have to put into an X开发者_Python百科ML file. So I decided I'd use php to make it. My code works, except where ALL the XML code is, it doesn't show because it's trying to render it. Help?

        $arg=explode("\n", $strang);
        echo count($arg);
        for ($i=0;$i<=count($arg);$i=$i+3)
        {
            echo "<word id='" . $arg[$i+1] . "'>";
            echo "<desc>" . $arg[$i] . " - " . $arg[$i+2] . "</desc>";
            echo "<pic></pic>";
            echo "<audio></audio>";
        }


I assume by render it you mean in your browser? If so, you'll need to escape the characters so they will be interpreted literally rather than as markup.

Check out htmlspecialchars and htmlentities


use CDATA construction:

echo "<desc><![CDATA[" . $arg[$i] . " - " . $arg[$i+2] . "]]></desc>";


If this is your entire script, fastest way would probably be to swap all of the <'s with &lt;

    $arg=explode("\n", $strang);
    echo count($arg);
    for ($i=0;$i<=count($arg);$i=$i+3)
    {
        echo "&lt;word id='" . $arg[$i+1] . "'>";
        echo "&lt;desc>" . $arg[$i] . " - " . $arg[$i+2] . "&lt;/desc>";
        echo "&lt;pic>&lt;/pic>";
        echo "&lt;audio>&lt;/audio>";
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号