如何计数数字字符和字符串字符来检查用户名长度值


PHP - how i count numerical character and string character to check username length value?

如何在php中计数数字字符与string字符组合?如果我用strlen,那只算string。我想限制username输入值只有20个字符,如果我输入20个或更多的string只,这个代码工作,但如果我输入(例如:Admin123Admin123Admin123)那不工作,我的验证输入失败。

我有一个代码在yii 2 useraccount控制器像这样:

        // new user
        if ( $username != '' && $password != '' && intval($group) > 0 && !$exist)
        {
            $myFunctions    = new userFunctions;
            $exist = $myFunctions->isUserNameExist( $username );
            $isValid = $myFunctions->isValidPassword( $password );
            $checkUsername = strlen($username);
            // $temp = str_split($username); // Convert a string to an array by each character
            // // if don't want the spaces
            // $temp = array_filter($temp); // remove empty values
            // $checkUsername = count($temp);
            if ( $isValid == 0 && !$exist)
            {
                $result = $myFunctions->saveNewUser( $username, $password, $group, $expired );
                $error =  ( $result ) ? 0 : 1;
            } 
            else if( $exist )
            {
                $error = 3;
            }
            else $error = 2;
        }
    }
    echo 'yii'helpers'Json::encode(['result' => $result, 'error' => $error, 'checkUsername' => $checkUsername ]);

这是我的代码在视图:

                function saveNewUsers()
                {
                    $.ajax({
                                type     :'POST',
                                dataType : 'json',
                                data     : { id: $('#hiUserID').val(), username : $('#txtUsername').val(), password: $('#txtPassword1').val(), group: $('#cbUserGroup').val(), expired: $('#cbExpired').val() },
                                url  : '" . 'Yii::$app->getUrlManager()->createAbsoluteUrl('useraccount/saveuser') . "',
                                success  : function(response) {
                                    if ( !response.result ) {
                                        if ( response.error == 2 )
                                        {
                                            $('#errorMessageUser').html(DecodeEntities('{$myLabels[20]}.')).show();
                                        }
                                        else if( response.error == 3 )
                                        {
                                            $('#errorMessageUser').html(DecodeEntities('{$myLabels[56]}.')).show(); 
                                        }
                                        else if( response.checkUsername > 20)
                                        {
                                            $('#errorMessageUser').html(DecodeEntities('{$myLabels[57]}.')).show();     
                                        }
                                        else $('#errorMessageUser').html(DecodeEntities('{$myLabels[22]}.')).show();
                                    }
                                    else {      
                                        $('#errorMessageUser').html('').hide();
                                        $('#myUserModal').modal('hide');
                                        $.pjax.reload({container:'#myPjax',timeout:false});

                                    }   
                                }                       
                    });
                }

那么,如何计数数值和字符串在php?我真的是PHP的新手,谢谢你的帮助,我希望从我们的程序员这里得到建议。对不起,我的英语不好。

我已经找到了答案,是的,感谢Chris 85, strlen不是问题,但问题是在控制器,这是我改变我的代码:

if ( $username != '' && $password != '' && intval($group) > 0 && !$exist)
        {
            $myFunctions    = new userFunctions;
            $exist = $myFunctions->isUserNameExist( $username );
            $isValid = $myFunctions->isValidPassword( $password );
            $checkUsername = strlen($username);
            // var_dump($checkUsername); die();
            if ( $isValid == 0 && !$exist && $checkUsername <= 20)
            {
                $result = $myFunctions->saveNewUser( $username, $password, $group, $expired );
                $error =  ( $result ) ? 0 : 1;
            }
            elseif ($checkUsername > 20 ) 
            {
                $error = 99;
            } 
            else if( $exist )
            {
                $error = 3;
            }
            else $error = 2;
        }

和视图中如下所示:

function saveNewUsers()
                {
                    $.ajax({
                                type     :'POST',
                                dataType : 'json',
                                data     : { id: $('#hiUserID').val(), username : $('#txtUsername').val(), password: $('#txtPassword1').val(), group: $('#cbUserGroup').val(), expired: $('#cbExpired').val() },
                                url  : '" . 'Yii::$app->getUrlManager()->createAbsoluteUrl('useraccount/saveuser') . "',
                                success  : function(response) {
                                    if ( !response.result ) {
                                        if ( response.error == 2 )
                                        {
                                            $('#errorMessageUser').html(DecodeEntities('{$myLabels[20]}.')).show();
                                        }
                                        else if( response.error == 3 )
                                        {
                                            $('#errorMessageUser').html(DecodeEntities('{$myLabels[56]}.')).show(); 
                                        }
                                        else if( response.error == 99)
                                        {
                                            $('#errorMessageUser').html(DecodeEntities('{$myLabels[57]}.')).show();     
                                        }
                                        else $('#errorMessageUser').html(DecodeEntities('{$myLabels[22]}.')).show();
                                    }
                                    else {      
                                        $('#errorMessageUser').html('').hide();
                                        $('#myUserModal').modal('hide');
                                        $.pjax.reload({container:'#myPjax',timeout:false});
                                    }   
                                }                       
                    });
                }

谢谢你的帮助,我终于得到了答案。