當我使用htmlentities()
對變量進行編碼時,它的工作原理與魅力類似,但如果我對數組做同樣的事情,它只會返回一個空數組。我試圖使用array_map()
但它是一樣的故事。我試圖將編碼切換到ISO-8859-1
和UTF-8
,但沒有成功。它不想工作。htmlentities()在與數組一起使用時返回空字符串
下面的代碼:
<html>
<head>
<title>Signup</title>
</head>
<body>
<form name="signup" method="POST" action="form.php">
<fieldset>
<legend><p style="color:red; font-size:16px">Sports</p></legend>
<ul>
<li>
<input type="checkbox" name="sports[]" value="soccer">
<label for="soccer">Soccer</label>
</li>
<li>
<input type="checkbox" name="sports[]" value="water_polo">
<label for="water_polo">Water polo</label>
</li>
<li>
<input type="checkbox" name="sports[]" value="tennis">
<label for="tennis">Tennis</label>
</li>
<li>
<input type="checkbox" name="sports[]" value="volleyball">
<label for="volleyball">Volleyball</label>
</li>
</ul>
</fieldset>
</form>
<?php
$sports = htmlentities($_POST["sports"], ENT_COMPAT, 'ISO-8859-15');
$count = count($sports);
if($count == 0) {
echo "You don't play any sports.<br>";
} else {
echo "You like playing: ";
foreach($sports as $s) {
if(--$count == 0) {
echo "<span style='color:red'>$s</span>.<br>";
break;
} else {
echo "<span style='color:red'>$s</span>, ";
}
}
}
?>
</body>
</html>
它產生以下輸出:
You don't play any sports.
含義ヶ輛()是不是能夠編碼我的數組。
表單/輸入是什麼樣的?以及你如何使用'array_map()'? –
['htmlentities()'](http://php.net/manual/de/function.htmlentities.php)不接受數組。 – simon
@simon我懷疑OP是否理解德語;-)引用PHP.net時使用英文鏈接http://php.net/manual/en/function.htmlentities.php –