Problem Array Item Problem, Cannot work with each item in array

Discussion in 'PHP' started by blug, Sep 5, 2009.

  1. #1
    Hello,

    I have been working for this basic function for 4-5 hours. I have changed it using differnet functions but it is not working.

    What i try to do is to collect some datas from a form, and output the data
    that are consisting of only only letters.

    Problem is the function i have written is working properly if i do the regexp checking in each array myself, but it is not in working properly, only checks the latest item in foreach, the others not.

    Please let me know where i am doing the mistake or gonna eat the nuts.

    What i am doing is :
    1) split all items that comes from textbox by newline
    2) in a loop , i check each data if it has only a-z letters in it. (the regexp i iplemented working properly alread when i check myself any string)


    <form action='index.php' method='POST'> 
    <textarea name="FILTER" cols=10 rows=10></textarea> 
    
    <input type='hidden' name='action' value='validate'> 
    <p> 
    <input type='submit' value='Submit'> 
    </form> 
    <?php 
    
    $str= $_REQUEST['FILTER'];
    $FILTER = explode("\n", $str);
    
    //echo "WRITING THE ALL DATA WITHOUT FILTERING : <br>";
    //echo "KeyID | Value<br>";
    
    foreach($FILTER as $key => $value) {
    //	echo $key . ":" . $value . "<br>";
    	if (eregi('^[a-zA-Z]+$', $value)) { 
    		echo 'FILTERED:'. " " .$value . "<br>";
    	}	
    }
    
    ?>
    
    PHP:

     
    blug, Sep 5, 2009 IP
  2. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #2
    do a var_dump($FILETER) before the foreach just to be sure the array is properly filled.
     
    caprichoso, Sep 5, 2009 IP
  3. blug

    blug Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    the array is properly filled. Because i can echo each lines without problem.
     
    blug, Sep 6, 2009 IP
  4. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Can you post the var_dump output here?
     
    caprichoso, Sep 6, 2009 IP
  5. ceban

    ceban Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Before exploding the string, assure that you have converted different new lines types to unix standard.
    
    $str = preg_replace('/\r\n|\r/', "\n", $str);
    $FILTER = explode("\n", $str);
    Code (php):
     
    ceban, Sep 6, 2009 IP