[英]PHP counting uppercase letters (not working)
我是PHP新手,尝试计算文本区域中的所有大写字母,以为单击“提交”按钮后我什么也没得到。 这是我的代码:
<!DOCTYPE html>
<html>
<body>
<?php
if(isset($_POST['submit'])) {
function caps($s) {
$u = 0;
$d = 0;
$n = strlen($s);
for ($x=0; $x<$n; $x++) {
$d = ord($s[$x]);
if ($d > 64 && $d < 91) {
$u++;
}
}
return $u;
}
$n1=$_POST['n1'];
echo 'caps: ' . caps($n1) . "\n";
}
?>
<form><textarea rows="4" cols="50" name="n1" value="<?php if(isset($_POST['n1'])){echo htmlspecialchars($_POST['n1']);}?>"></textarea>
<br><input type="submit" name="submit" value="Submit"></form>
</body>
</html>
本示例将为您提供帮助。
preg_match_all("/[A-Z]$/", $s, $matches);
$all_upper_cases = count($matches);
使用此功能:
function count_capitals($s) {
return strlen(preg_replace('![^A-Z]+!', '', $s));
}
恩。
$n1=$_POST['n1'];
echo 'caps: ' . count_capitals($n1) . "\n";
文本框:
<textarea rows="4" cols="50" name="n1" value="<?php count_capitals($n1) ?>"></textarea>
您忘记设置form 方法来发布代码,应该是这样的:
<!DOCTYPE html>
<html>
<body>
<?php
if(isset($_POST['submit'])) {
function caps($s) {
$u = 0;
$d = 0;
$n = strlen($s);
for ($x=0; $x<$n; $x++) {
$d = ord($s[$x]);
if ($d > 64 && $d < 91) {
$u++;
}
}
return $u;
}
$n1=$_POST['n1'];
echo 'caps: ' . caps($n1) . "\n";
}
?>
<form method="post"><textarea rows="4" cols="50" name="n1" value="<?php if(isset($_POST['n1'])){echo htmlspecialchars($_POST['n1']);}?>"></textarea>
<br><input type="submit" name="submit" value="Submit"></form>
</body>
</html>
确保将表单方法设置为post 。 如果不提供方法,则表单将使用get方法而不是post。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.