Check string length in PHP
Try the common syntax instead:
if (strlen($message)<140) {
echo "less than 140";
}
else
if (strlen($message)>140) {
echo "more than 140";
}
else {
echo "exactly 140";
}
Checking string length, max and minimum
I guess you can make a function like this:
function validStrLen($str, $min, $max){
$len = strlen($str);
if($len < $min){
return "Field Name is too short, minimum is $min characters ($max max)";
}
elseif($len > $max){
return "Field Name is too long, maximum is $max characters ($min min).";
}
return TRUE;
}
Then you can do something like this:
$errors['field_name'] = validStrLen($field, 12, 20);
php get string length and trim to get only specific string
You are looking for substr()
$string = "hello world how are you";
if (strlen($string) > 11) {
echo substr($string,0,11);
}
This will result to:
hello world
Doc: http://php.net/manual/en/function.substr.php
Checking string length with strlen
if ($len != 32 && $len != 40)
try that out
Verify string has length greater than 0 and is not a space in PHP
Assuming your string is in $string
:
if(strlen(trim($string)) > 0){
// $string has at least one non-space character
}
Note that this will not allow any strings that consist of just spaces, regardless of how many there are.
If you're validating inputs, you might want to think about other degenerate cases, too, like someone entering just an underscore, or other unsuitable input. If you tell us more about the situation you're trying to deal with we might be able to provide more robust checking.
does isset php function checks string length
The isset()
method here just verify if $value[4]
(your object, or string, or other) exists or not.
php check string has no space and length greater than limit
A better solution would be to use
word-wrap: break-word;
It will break the string when it gets too long
Measure string size in Bytes in php
You have to figure out if the string is ascii encoded or encoded with a multi-byte format.
In the former case, you can just use strlen
.
In the latter case you need to find the number of bytes per character.
the strlen documentation gives an example of how to do it : http://www.php.net/manual/en/function.strlen.php#72274
PHP Check If String Contains Numbers and Check String Length
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:
str_pad()
: http://php.net/str_padctype_digit()
: http://www.php.net/manual/en/function.ctype-digit.php
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);
}
Related Topics
Get the Maximum Value from an Element in a Multidimensional Array
How to Find a Whole Word in a String in PHP Without Accidental Matches
How to Store and Retrieve Image Contents from the Database Using Laravel
Single and Double Quotes Together as HTML Attribute Value
Sending Multiple Attachment in an Email Using PHP
Getting All Dates for Mondays and Tuesdays for the Next Year
What Is the Use of '\G' Anchor in Regex
Unset a Column in a Multidimensional Array
Select Count() VS MySQL_Num_Rows();
Store Procedures in PHPmyadmin
Php, Simplexml, Decoding Entities in Cdata
How to "Validate" Human Names in Cakephp
PHP Keep Checkbox Checked After Submitting Form
Will a Script Continue to Run Even After Closing a Page
Passing JavaScript Variable to PHP Using Ajax
How to Check If an Uploaded File Is an Image Without Mime Type