doulao3905 2014-06-15 22:48
浏览 26
已采纳

PHP:filter_var没有过滤[关闭]

<?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;
?>

It returns:

Hello, HOw Are you?
:email@gmail.com
38u

It SHOULD be returning:

hello, how are you?
email@gmail.com
38

Does anyone see what I'm missing here? I know the methods are at least passing the variables through, and the trim() function is working just fine.

  • 写回答

1条回答 默认 最新

  • duanbi3385 2014-06-15 22:52
    关注

    Functions like trim(), filter_var() and strtolower() don't modify the string itself, instead they return a copy. You need to store that return value in a variable:

    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);
    }
    

    Similarly in clean_email() and clean_int().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题