1

我知道我应该对所有表单文本输入字段使用 htmlentities 但这不起作用:

<?php
echo "<tr>
        <td align=\"right\">".Telephone." :</td>    
        <td><input type=\"text\" name=\"telephone\" size=\"27\"
            value=\"htmlentities($row[telephone])\"> Inc. dialing codes
        </td>    
</tr>";
?>

它只是在表单中将输入值显示为“htmlentities(0123456789)”?请问我做错了什么?

4

7 回答 7

5

尝试使用

value=\"" . htmlentities($row[telephone]) . "\"

那里。目前,您的字符串仅包含 htmlentities 字符串并将变量拼接到其中。您需要取出字符串,调用函数并将其结果放在适当的位置,如上所述。

于 2008-10-16T16:08:14.033 回答
3

您不能在字符串中间调用函数。您需要从函数调用中获取返回值,然后将其包含在字符串中。

然而...

<tr>
    <td align="right">
        <label for="telephone">Telephone:</label>
    </td>    
    <td>
        <input type="text" 
               name="telephone" 
               id="telephone"
               size="27" 
               value="<?php 
                   echo htmlentities($row[telephone]); 
               ?>"> 
        Inc. dialing codes 
    </td>
</tr>

...会更干净。

就像摆脱不推荐使用的表示标记和使用表格进行布局一样。

于 2008-10-16T16:08:37.650 回答
1

@workmad3: that won't work as he's doing PHP.

<?php echo '<tr>
                <td align="right">' . Telephone . ' :</td>    
                <td><input type="text" name="telephone" size="27" value="' . htmlentities($row[telephone]) . '" /> Inc. dialing codes</td>    
        </tr>';
于 2008-10-16T16:10:31.033 回答
1

This will work:

<?php
echo "    <tr>
                    <td align=\"right\">Telephone :</td>    
                    <td><input type=\"text\" name=\"telephone\" size=\"27\" value=\"".htmlentities($row[telephone])."\"> Inc. dialing codes</td>    
            </tr>";
?>

BTW, I also corrected some very strange syntax you have going on here, like where you concatenate the constant "Telephone", which really should be inside the string. These kinds of details are important and will break your code easily.

Also, I suggest using single quotes, instead of double, around a string like this so that you don't have to escape all of the double quotes inside the string.

于 2008-10-16T16:15:25.423 回答
1

如果您只是在寻找使您的输出在 hml 中安全;您应该改用 htmlspecialchars(),因为它“只是”一个电话号码。

htmlspecialchars($row[telephone], ENT_QUOTES);

htmlentities() 有点慢,而且对多字节字符不太好。但我猜你并没有解决这些问题只是喷气机。

于 2008-10-16T20:03:20.237 回答
1

如果您想组合一大段 HTML 和 PHP 变量,您可以做两件事。

一,使用HEREDOC结构。

$txt = <<<HERETEXT
Put your HTML here.
HERETEXT;

echo $txt;

其次,使用第一个类变量来命名函数,然后在 HEREDOC 中使用它。

$he = 'htmlentities';

$txt = <<<HERETEXT
{$he($string, ENT_QUOTES, 'UTF-8')}
HERETEXT;

echo $txt;

然而,HTML 不应该以非常大的块来处理,因为会增加出现严重错误的风险。此外,您可能会不必要地重复自己。

于 2012-09-30T02:39:19.857 回答
0

首先,不要在字符串中回显您的 HTML。将代码与标记分开。

<tr>
    <td align="right">Telephone :</td>
    <td><input type="text" name="telephone" size="27"
        value="<?php echo htmlentities($row['telephone']); ?>"> Inc. dialing codes</td>
</tr>
于 2008-10-16T16:09:32.627 回答