SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2009
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Looking for a shorthand way of checking strlen for all text fields

    Is there a quicker and and less verbose way of checking a number of submitted post variables for a maximum string length?

    I currently use a strlen rule for each variable, which happen to have the same max length, separated by and/or operators.

    (I think that I have used the correct terms, but if not, please let me know if anything isn't clear).

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can you post your current solution?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Evangelist Waffles's Avatar
    Join Date
    Nov 2005
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well you can set a maximum character size on the text fields first, but you could look in to

    PHP Code:
    foreach ($_POST as $key => $value) {
        
    // do whatever
     


  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    What I mostly do is something like

    PHP Code:
    $allValid true;
    $threshold 255;
    foreach(
    $_POST as $var)
    {
       
    $allValid $allValid && ( strlen($var) < $threshold );

    The parentheses around strlen($var) < $threshold are not syntactically necessary but I like them for readability.

    If you really want to know if all is good and stop as soon as one check fails you could also do

    PHP Code:
    $allValid true;
    $threshold 255;
    foreach(
    $_POST as $var)
    {
       
    $allValid $allValid && ( strlen($var) < $threshold );
       
    // stop checking if one if the variables is too long
       
    if (!$allValid) break;

    The break statement tells PHP to stop the foreach loop and go on with what's beneath it (in this example nothing, but you get the point).

    If you want different thresholds for different variables you could do something like

    PHP Code:
    $allValid true;
    // the next line is just an example of course
    $checks = array('firstname' => 40'lastname' => 40'email' => 60);
    foreach(
    $checks as $var => $threshold)
    {
       
    $allValid $allValid && ( strlen($var) < $threshold );

    Hope that helps

    PS. I guess you already know this, but
    < means smaller than
    <= means smaller than or equal to
    Same goes for > and >=
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2009
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much Scallio. That helps.

    Thanks to Waffles too, but your example isn't clear to me.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •