Question

I'm trying to check if a username string is all numbers (but the field is not a numeric field). If so, I need the string to be 5 characters in length by adding leading 0's.

I have this code:

<?php
$getuname = $_GET['un'];

if (mb_strlen($getuname) <= 1){
        $getuname = "0".$getuname;
    }
if (mb_strlen($getuname) <= 2){
        $getuname = "0".$getuname;
    }
if (mb_strlen($getuname) <= 3){
        $getuname = "0".$getuname;
    }
if (mb_strlen($getuname) <= 4){
        $getuname = "0".$getuname;
    }   
echo $getuname;
?>

The above code works for making sure the string is 5 characters by adding zeros, but it's not very pretty and I'm sure there is a much nicer way to do it. Anyone?

Also, this whole piece needs to be wrapped in an IF statement that is checking to see if it contains nothing but numbers in the first place. I tried using !is_numeric, but this doesn't seem to work. I'm assuming because it's not a numeric type field.

Was it helpful?

Solution

You can use is_numeric to check for a numeric field, it doesn't matter whether the variable is a string or not. See example here.

Then you can simply pad using str_pad()

if(!is_numeric($test))
    echo $test . ' is not a number!';
else
    echo str_pad($test, 5, 0, STR_PAD_LEFT);

Edit: as flixer pointed out, is_numeric() won't actually do what you specifically asked (to check that a string contains only digits, i.e. no periods, commas, dashes etc which would be considered to "be a number"). In this case, use ctype_digit() instead:

$test_number = '123.4';
$test_number2 = '12345';

echo ctype_digit($test_number) ? $test_number . ' is only digits' : $test_number . ' is not only digits';
echo ctype_digit($test_number2) ? $test_number2 . ' is only digits' : $test_number2 . ' is not only digits';

// output:
// 123.4 is not only digits
// 12345 is only digits

The key here is to avoid regex when you have better tools to do the job.

To add a little to this, ctype_digit() might return false when you pass in an integer variable: (example from PHP manual)

ctype_digit( '42' ); // true
ctype_digit( 42 ); // false - ASCII 42 is the * symbol

This can be OK, depending on the situation you're using this in. In your case you're validating a $_GET variable, which is always going to be a string so it won't affect you.

Docs:

OP Here, this is it all together. Works like a charm...

   if (ctype_digit($getuname) == true) {
    $getuname = str_pad($getuname, 5, 0, STR_PAD_LEFT);
    }

OTHER TIPS

Use the str_pad() function. Like this:

$getuname = str_pad($_GET['un'], 5, '0', STR_PAD_LEFT);

Try this:

<?php
$getuname = $_GET['un'];
if(ctype_digit($getuname))
     $getuname = str_repeat("0", 5-strlen($getuname)) . $getuname;
?>

Hope it works for u.

This should be a clean solution:

$getuname = $_GET['un'];
if(preg_match('/^\d+$/',$getuname)){
    echo sprintf('%05d', $getuname);
}else{
    // incorrect format
}
<?php
$getuname = $_GET['un'];


while (strlen($getuname) < 5) {
    $getuname = '0' . $getuname;
}

echo $getuname;

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top