Posted By

scopefragger on 09/02/09


Tagged

count validate limit characters varable


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

umang_nine
scopefragger


Count Varable character lengh


 / Published in: PHP
 

URL: http://www.scopesden.co.uk/code_get_feed.php?Content_ref=14

Sometimes counting the length of a variable is a necessity, such as when a new user registers. You may restrict a limit of 10 characters as a username or password, Using the following php function you can count the length of a variable.

In additionally I have provided an example ‘If’ statement that could be used in a username length validation.

  1. $varable = 'thisisatest';
  2. echo strlen($varable); // 11
  3.  
  4. $varable = '1234567';
  5. echo strlen($varable); // 7
  6. ?>
  7.  
  8.  
  9. //Nof for an 'if' example
  10. if(strlen($username)>8){$validate=='fail'}
  11. else
  12. {$validate=='pass'}

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: kirik on September 2, 2009

Actually you should use regular expression for username validation: if (preg_match('/^[a-z]{8,}$/i', $username)) { echo 'OK'; } else { exit('Wrong username'); }

Posted By: scopefragger on September 3, 2009

true, however when typing this i was short of examples so username lengh validation was a nice friendly introduction to strlen. More to the point a good example of strlen would be the function that will be used in http://www.scopesden.co.uk/codegetfeed.php?Content_ref=14 to evaluate the size of the text feed and to manipulate the number of rows required so that 1, the page isnt epic huge for a single liner code and secondly so that a 200 line code isnt squeesed into a 1 row box ( there extreamaties but you get the point ). Other than that thogh i havent realy used strlen.

Posted By: scopefragger on September 3, 2009

true, however when typing this i was short of examples so username lengh validation was a nice friendly introduction to strlen. More to the point a good example of strlen would be the function that will be used in http://www.scopesden.co.uk/codegetfeed.php?Content_ref=14 to evaluate the size of the text feed and to manipulate the number of rows required so that 1, the page isnt epic huge for a single liner code and secondly so that a 200 line code isnt squeesed into a 1 row box ( there extreamaties but you get the point ). Other than that thogh i havent realy used strlen.

Posted By: bucabay on September 3, 2009

If you're using PHP4, using strlen() will only work for non-multibyte encoded strings. If you have a multibyte encoding such as UTF-8, then mb_strlen() should be used, or a compatible function. Almost all the PHP4 string functions suffer from this problem.

nb: "Varable" is spelled variable.

Bucabay

You need to login to post a comment.