Lenght of strings in an array!

Discussion in 'PHP' started by Justcrapx, Nov 25, 2006.

  1. Justcrapx

    Justcrapx New Member

    Joined:
    Oct 30, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Can someone gimme the idea to check if any string elements of an array are longer than "x" characters.

    In other words, pretend that theres an array which we dont know how many elements it has. And this array only keeps strings. All i want is a code that returns false if theres any long string than 20.
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Simple! Here is the code!

    PHP:
    <?
     function 
    check_array_string(&$arr,$length=10)
     {
         foreach(
    $arr as $v)
             if(
    strlen($v)>$length)
                 return 
    false;
         return 
    true;
     }
     
    ?>
    Example:
    PHP:
    $a = ["sas","aasew","wewr"];
    check_array_string($a,20);
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice