从PHP

时间:2015-08-16 00:02:08

标签: php variables escaping

我有一个字符串,使用以下代码

写入.txt文档
$myfile = fopen("SomeFolder/Cards.txt", "w");
        $txt = "$CardDetail";
        fwrite($myfile, $txt);
        fclose($myfile); 

$txt的字符串来自变量$CardDetail

这就是$CardDetail变量的样子

$CardDetail = "

        <div id='CardContain'>
        <iframe id='No' width='364.8' height='220.8' scrolling='no'  frameBorder='0'  src='../Cards/ID/$ID/Iframe2.php'>​</iframe>
        <div id='CardWrapping'>
        <h1 id='Cardtext'><b>CARD ID</b> $ID</h1>
        <h1 id='Cardtext'><b>LAST EDITED</b> $Edited<a id='EditLink' href='../Cards/ID/$ID/Editcard.php'> (edit card)</a></h1>
        <h1 id='Cardtext'><b>DESCRIPTION</b> $Description</h1>
        </div>
        <?php if($KeepVariable == "Exsample"){ echo "<div id='CardStats'><h1 id='Cardtext1'>Standby</h1></div>"; }else{ echo "
        <a href='Selectpayment.php?CardID=$ID'><div id='CardButton'><h1 id='Cardtext1'>Options</h1></div></a> ";} ?>

        </div>
";

我需要$CardDetail中该字符串的以下部分不要执行,只需将其写入Cards.txt文档即可。

<?php if($KeepVariable == "Exsample"){ echo "<div id='CardStats'><h1 id='Cardtext1'>Standby</h1></div>"; }else{ echo "
            <a href='Selectpayment.php?CardID=$ID'><div id='CardButton'><h1 id='Cardtext1'>Options</h1></div></a> ";} ?>

我根本不希望任何人理解我在这里要解释的内容。但简单来说,我需要排除字符串中的那部分内容。

1 个答案:

答案 0 :(得分:1)

\转义变量,如下所示:

$CardDetail = "

        <div id='CardContain'>
        <iframe id='No' width='364.8' height='220.8' scrolling='no'  frameBorder='0'  src='../Cards/ID/$ID/Iframe2.php'>​</iframe>
        <div id='CardWrapping'>
        <h1 id='Cardtext'><b>CARD ID</b> $ID</h1>
        <h1 id='Cardtext'><b>LAST EDITED</b> $Edited<a id='EditLink' href='../Cards/ID/$ID/Editcard.php'> (edit card)</a></h1>
        <h1 id='Cardtext'><b>DESCRIPTION</b> $Description</h1>
        </div>
        <?php if(\$KeepVariable == \"Exsample\"){ echo \"<div id='CardStats'><h1 id='Cardtext1'>Standby</h1></div>\"; }else{ echo \"
        <a href='Selectpayment.php?CardID=$ID'><div id='CardButton'><h1 id='Cardtext1'>Options</h1></div></a> \";} ?>

        </div>
";