如何解析字符串或HTML内容在“;回声”;


How to parse strings or HTML content in "echo"

我有HTML内容(一个表单),只有在满足条件时才会显示,并将由echo方法显示。我正在尝试解析PHP中的代码。我收到语法错误,我不知道如何处理,因为我需要解析submit=""value=""等值旁边的值。下面是一个例子:

if(isset($_GET['handle']) && !isset($_GET['serial_key']))
{
  $allowtoEnterSN='True';   
  $a_handle= mysql_escape_string($_GET['handle']);
  echo '          <span class="activation_bold">Please activate your account to continue.</span></p>
          <form id="activate" name="activate" method="post" action="activate_check.php?handle='.$_POST['a_handle'].'&amp;serial_key='.$_POST['serial_key'].'">
            <p class="activation_reg">Please enter your Serial Key Number to activate.</p>
            <p class="activation_reg">
              <label for="user_name">Handle</label>
              <input name="user_name" type="text" class="activation_reg" id="user_name" />
            </p>
            <p class="activation_reg">
              <label for="serial_key">Serial Key Number</label>
              <input name="serial_key" type="text" class="activation_reg" id="serial_key" size="40" />
              <input name="submit" type="submit" class="a_button" id="submit" value="Activate" />
              <input name="a_handle" type="hidden" id="a_handle" value="<? echo $a_handle ?>" />
            </p>
          </form>'  
}   

最新更新

if(isset($_GET['handle']) && !isset($_GET['serial_key']))
{
  $allowtoEnterSN='True';   
  $a_handle= mysql_escape_string($_GET['handle']);
  echo <<<HTML
  <p>
<span class="activation_bold">Please activate your account to continue.</span></p>
<form id="activate" name="activate" method="post" action="activate_check.php?handle={$_POST['a_handle']}&amp;serial_key={$_POST['serial_key']}">

        <p class="activation_reg">
          <label for="serial_key">Serial Key Number</label>
          <input name="serial_key" type="text" class="activation_reg" id="serial_key" size="40" />
          <input name="submit" type="submit" class="a_button" id="submit" value="Activate" />

          <input name="a_handle" type="hidden" id="a_handle" value="$a_handle" />
        </p>
      </form>
HTML;}  

上次更新于1634ct

if(isset($_GET['handle']) && !isset($_GET['serial_key']))
{
  $allowtoEnterSN='True';   
  $a_handle= mysql_escape_string($_GET['handle']);
  echo <<< EOD 
  <span class="activation_bold">Please activate your account to continue.</span></p>
          <form id="activate" name="activate" method="post" action="activate_check.php?handle='.$_POST['a_handle'].'&amp;serial_key='.$_POST['serial_key'].'">
            <p class="activation_reg">Please enter your Serial Key Number to activate.</p>
            <p class="activation_reg">
              <label for="user_name">Handle</label>
              <input name="user_name" type="text" class="activation_reg" id="user_name" />
            </p>
            <p class="activation_reg">
              <label for="serial_key">Serial Key Number</label>
              <input name="serial_key" type="text" class="activation_reg" id="serial_key" size="40" />
              <input name="submit" type="submit" class="a_button" id="submit" value="Activate" />
              <input name="a_handle" type="hidden" id="a_handle" value="<? echo $a_handle ?>" />
            </p>
          </form>
EOD;    
}           

您应该使用HEREDOC,而不是单引号字符串,它将插入您需要的变量。将复杂变量封装在{}中。

echo<<<HTML
<span class="activation_bold">Please activate your account to continue.</span></p>
      <form id="activate" name="activate" method="post" action="activate_check.php?handle={$_POST['a_handle']}&amp;serial_key={$_POST['serial_key']">
        <!-- SNIP -- >
        <p class="activation_reg">
          <label for="serial_key">Serial Key Number</label>
          <input name="serial_key" type="text" class="activation_reg" id="serial_key" size="40" />
          <input name="submit" type="submit" class="a_button" id="submit" value="Activate" />
          <!-- now just use $a_handle -->
          <input name="a_handle" type="hidden" id="a_handle" value="$a_handle" />
        </p>
      </form>
HTML;

附录:HEREDOC:速成课程

HEREDOC是一个多行字符串,它提供格式并表现得像一个双引号字符串,相应地插入PHP变量。

要开始HEREDOC,请使用<<<运算符,后跟一些标识符(本例中为VAR)。在它自己的行的开头使用相同的标识符(VAR)结束它,没有缩进,后面跟着分号(VAR;)。如果缩进或后面跟着空白,它将无法正常工作。

$heredoc_var =<<<VAR
  Now you can type whatever you want including $variables.
  And over multiple lines.
VAR;  <--- No extra whitespace here and must be at the beginning of the line!!!!

我不确定这是否回答了你的问题,但这里有一个错误:

 <span class="activation_bold">Please activate your account to continue.</span></p>

没有启动<p>

编辑:另外看看达米恩·皮尔西对另一个问题的回答。

<input name="a_handle" type="hidden" id="a_handle" value="<? echo $a_handle ?>" />

你在一个单引号字符串中写echo,这是行不通的。应为:

'.....<input name="a_handle" type="hidden" id="a_handle" value="'.$a_handle.'" />....'

问题:为什么要使用$a_handle= mysql_escape_string($_GET['handle']);?他们以后会侵入数据库吗?

  1. 如果是这种情况,请使用mysql_real_escape_string
  2. 否则,如果您正在寻找一个转义,它可能是一个html转义(针对XSS攻击)。您至少应该使用htmlentities($_GET['handle'])

我不确定这是否是您唯一的问题,但是:</form>'后面缺少一个分号。应该是</form>';

我还建议您像其他答案所建议的那样使用HEREDOC符号。它对长弦来说要干净得多。此外,正如其他人所说,如果你不能用htmlentitiesurlencode(取决于文本在HTML中的位置)逃脱输出,你就会面临XSS攻击。请参阅:http://en.wikipedia.org/wiki/Cross-site_scripting