<?php
class validation {
function clean_txt($str, $letter_case="") {
trim($str);
filter_var($str, FILTER_SANITIZE_STRING);
switch ($letter_case) {
case "L":
strtolower($str);
break;
case "U":
strtoupper($str);
break;
case "T":
ucwords($str);
break;
default:
$str="";
}
return ($str);
}
function clean_email($email_var) {
filter_var($email_var, FILTER_SANITIZE_EMAIL);
return ($email_var);
}
function clean_int($int_var) {
trim($int_var);
filter_var($int_var, FILTER_SANITIZE_NUMBER_INT);
return ($int_var);
}
$functions = new validation();
$var = $functions->clean_txt(" Hello, HOw Are you?", "L");
echo $var;
?>
<br>
<?php
$var = $functions->clean_email(":zsdazey@gmail.com");
echo $var;
?>
<br>
<?php
$var = $functions->clean_int("38u");
echo $var;
?>
它返回:
你好,你好吗? :email@gmail.com
38u
它应该回来了:
你好,你好吗? email@gmail.com
38
有谁看到我在这里缺少的东西?我知道这些方法至少会传递变量,trim()
函数工作得很好。
答案 0 :(得分:2)
trim()
,filter_var()
和strtolower()
等功能不会修改字符串本身,而是返回副本。您需要将该返回值存储在变量中:
function clean_txt($str, $letter_case="") {
$str = trim($str);
$str = filter_var($str, FILTER_SANITIZE_STRING);
switch ($letter_case) {
case "L":
$str = strtolower($str);
break;
case "U":
$str = strtoupper($str);
break;
case "T":
$str = ucwords($str);
break;
default:
$str="";
}
return ($str);
}
同样适用于clean_email()
和clean_int()
。