PHP/String/strlen
Версия от 10:37, 26 мая 2010; (обсуждение)
Содержание
Calculating the length of a string
<?php
$password="password";
if (strlen($password) <= 5){
echo("Passwords must be at least 5 characters long.");
}
else {
echo ("Password accepted.");
}
?>
Checking String Length
<?php
define ("MAXLENGTH", 10);
if (strlen ("Hello World!") > MAXLENGTH){
echo "The field you have entered can only be " . MAXLENGTH . " characters in length.";
} else {
//Insert the field into the database.
}
?>
Checking String Length: int strlen ( string string )
<?php
define ("MAXLENGTH", 10);
if (strlen ("Hello World!") > MAXLENGTH){
echo "The field you have entered can be only " . MAXLENGTH . " characters in length.";
}
?>
Concisely checking the length of a trimmed string
<?
if (strlen(trim("12345")) != 5) {
print "Please enter a ZIP code that is 5 characters long.";
}
?>
int strlen ( string str ) returns the number of characters in it:
<?
print strlen("Foo") . "\n"; // 3
print strlen("Goodbye, Perl!") . "\n"; // 14
?>
strlen.php
<?php
$pswd = "secretpswd";
if (strlen($string) < 10) echo "Password is too short!";
?>