how to find out how many characters are in a string

RegisterLogin
how to find out how many characters are in a string
Post Description: how to find out how many characters are in a string
Tags: how, to, find, out, how, many, characters, are, in, a, string
This Post Was Posted On Feb 29, 2008 By web hosting #642
lets say you have a form, and in the form you have a field called year. the format for the year is four digits, for example: 2007

well, lets say you want to make sure that people don't put more than four digits into the year field. so if someone enters 20078, your script should reject the entry and display an error.

well, to find out how many characters are in a string, you can use the strlen() in php.

so, lets follow my example. lets say the your visitor has enter their information and the value of the year field is like this:

$year = 20079

as you can see, the number of characters in $year is 5, so how do you count the characters in a string, you can use this as an example:

<?
if(strlen($string) > 4) { echo 'Error. this field requires only 4 characters';}
?>


the above code just simple checks if the value of $string is more than 4 characters, it will display

Error. this field requires only 4 characters

hope this helps. if you need php webhosting, check out webune.com they have excellent service.
Leave Your Comments
Related Pages: [Add Your Website]
Post New Topic
©2012 Webune Forums - Tue Dec 18, 2012 10:41 am
Powered by: Webune Forums V3