Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    925
    Thanks
    76
    Thanked 29 Times in 29 Posts

    Searching an array - function

    Hi, I want to create a function which takes a form input as its only parameter. I want the function to search an array of characters and if that character is found within the user input, then the function returns true, if no characters are found within the user input I want the function to return false.

    Here is what I have made:
    PHP Code:
    function check_string($data){
        
    $chars = array('!','?','~','@','"','£','$','%','^','&','*','(',')','[',']','#','/','<','>','+','=','-','_','.',',',':',';','{','}','|');
        
        for(
    $i=1;$i<$chars;$i++){
           if(
    strstr($data$chars[$i])){
              
    $result TRUE;
              return 
    $result;
           }
           
        }
        
        
    $result FALSE;
        return 
    $result;
        
    }

    $result check_string($_POST['user']);
    echo 
    $result
    If I enter any characters in the array into the form field, it echo's "1", and if I do not enter any of those characters, it gives errors. It doesn't reload, it just stays loading forever and crashes.

    Does anyone know what I am doing wrong?

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    80
    Thanks
    0
    Thanked 13 Times in 12 Posts
    It probably gets stuck in an infinite look because $chars is not a number but an array, you need to use the array size as your terminator.

    PHP Code:

    $count_chars 
    count$chars );

     for( 
    $i=1$i<$count_chars$i++ ){ 

  • #3
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    925
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Ah thank you very much.

    Can I also ask, you know in JavaScript when doing a for loop, you do:
    Code:
    for(var i=0;i<=someArray.length-1;i++){
    //code here....
    }
    Why won't PHP allow you to use the length property?

    Kind regards,

    LC.

  • #4
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    925
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Also could I just ask, when creating an action file, should I check the form fields with isset() BEFORE or AFTER sending the form field value to the check_string() function?

    Regards,

    LC.


  •  

    Posting Permissions

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