How To Find Out How Many Characters Are In A String



How To Find Out How Many Characters Are In A String
How To Find Out How Many Characters Are In A String
Post Description:
Post Tags: how, to, find, out, how, many, characters, are, in, a, string
This Post Has Been Viewed 374 Times Since Wed Jun 20, 2007 7:11 am Posted By hostman with 0 replies
How To Find Out How Many Characters Are In A String
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     [ dejar commentarios ]
  * Name     [nombre]

  * eMail (will not be published)     [coreo electronico]

* Enter Your Reply or Comments:    [commentarios]


Add Picture To Comments         [incluir foto]
YES NO             upload
Receive Replies on my Comments (An email will be sent to you when someone replies to your comments)

     

Comments and replies About How To Find Out How Many Characters Are In A String




(0) Comments