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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post

    PLEASE HELP! PHP get line from file and compare it not working.

    Hi. Can someone please make it so that this code that I made reads through every line in a file, and checks it against the $myvar variable to see if its equal to what its reading in the file?

    PHP Code:
    <?php

    /*** make sure the file exists ***/
    $file 'my_file.txt';

    $myvar "b";
    $couponcode readLine($file2);

    //2nd line of the text file should be b

    if( file_exists$file ) && is_readable$file ) )
    {
    if(
    $myvar != $couponcode){ //if b!=b
        
    echo $myvar."!=".$couponcode;
    }else{
        echo 
    $myvar."!";
    }
    }
    else
    {
        echo 
    "Cannot read from $file";
    }
    ?>

    <?php

    /**
     *
     * Read a line number from a file
     *
     * @param    string    $file    The path to the file
     * @param    int    $line_num    The line number to read
     * @param    string    $delimiter    The character that delimits lines
     * @return    string    The line that is read
     *
     */
    function readLine($file$line_num$delimiter="\n")
    {
        
    /*** set the counter to one ***/
        
    $i 1;

        
    /*** open the file for reading ***/
        
    $fp fopen$file'r' );

        
    /*** loop over the file pointer ***/
        
    while ( !feof $fp) )
        {
            
    /*** read the line into a buffer ***/
            
    $buffer stream_get_line$fp1024$delimiter );
            
    /*** if we are at the right line number ***/
            
    if( $i == $line_num )
            {
                
    /*** return the line that is currently in the buffer ***/
                
    return $buffer;
            }
            
    /*** increment the line counter ***/
            
    $i++;
            
    /*** clear the buffer ***/
            
    $buffer '';
        }
        return 
    false;
    }
    ?>
    ANY HELP is GREATLY appreciated!

    Shadow~
    Last edited by ShadowIce; 01-11-2010 at 05:03 PM.

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    $file  'my_file.txt';
    $myvar 'b';

    // Get all lines of the file as an array, ignoring new line characters and empty lines
    $lines file$fileFILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES );

    // does the value of $myvar exist in the array?
    if ( FALSE !== array_search$myvar$lines ) )
    {
        
    // at least one line in $file contains the value in $myvar

    Last edited by kbluhm; 01-11-2010 at 05:20 PM.

  • #3
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    Here's what I have so far:

    PHP Code:
    <?php

    /*** make sure the file exists ***/
    $file 'my_file.txt';

    $myvar "b";
    $couponcode readLine($file2);

    // Get all lines of the file as an array, ignoring new line characters and empty lines
    $lines file$fileFILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES );

    if( 
    file_exists$file ) && is_readable$file ) )
    {
    // does the value of $myvar exist in the array?
    if ( FALSE !== array_search$myvar$lines ) )
    {
    echo 
    $myvar;
    }else{
    echo 
    "variable incorrect";
    }
    }
    else
    {
        echo 
    "Cannot read from $file";
    }
    ?>

    <?php

    /**
     *
     * Read a line number from a file
     *
     * @param    string    $file    The path to the file
     * @param    int    $line_num    The line number to read
     * @param    string    $delimiter    The character that delimits lines
     * @return    string    The line that is read
     *
     */
    function readLine($file$line_num$delimiter="\n")
    {
        
    /*** set the counter to one ***/
        
    $i 1;

        
    /*** open the file for reading ***/
        
    $fp fopen$file'r' );

        
    /*** loop over the file pointer ***/
        
    while ( !feof $fp) )
        {
            
    /*** read the line into a buffer ***/
            
    $buffer stream_get_line$fp1024$delimiter );
            
    /*** if we are at the right line number ***/
            
    if( $i == $line_num )
            {
                
    /*** return the line that is currently in the buffer ***/
                
    return $buffer;
            }
            
    /*** increment the line counter ***/
            
    $i++;
            
    /*** clear the buffer ***/
            
    $buffer '';
        }
        return 
    false;
    }
    ?>

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    My example above replaces the need for that rather inefficient readLine() function... I'm not sure why you're attempting to combine the two... and you're now checking for the existence and readability of the file after you get the contents with file().

    You're also telling us what you have so far and nothing else. Is the script complete? Are there any errors? Are you looking for any additional advice or input?

    Try this if you'd like to grab the line number that matched:
    PHP Code:
    $file  'my_file.txt';
    $myvar 'b';

    // Get all lines of the file as an array, ignoring new line characters
    $lines file$fileFILE_IGNORE_NEW_LINES );

    // does the value of $myvar exist in the array?
    if ( FALSE !== ( $num array_search$myvar$lines ) ) )
    {
        
    // line number $num matched the value of $myvar

    You could also user array_keys() in the place of array_search() if more than one line will potentially contain the value of $myvar and you'd like to return them all. array_search() will only return the first match.
    Last edited by kbluhm; 01-11-2010 at 05:48 PM.

  • #5
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    Well, I incorperated your code into my if statement after checking to see if the file exists, and it is readable. I for some reason can't get it to work..

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by ShadowIce View Post
    Well, I incorperated your code into my if statement after checking to see if the file exists, and it is readable. I for some reason can't get it to work..
    Are you understanding the use of file()?

    Quote Originally Posted by kbluhm View Post
    Is the script complete? Are there any errors? Are you looking for any additional advice or input?
    I have to run to the gym and get pumped. I'll be back in an hour or so to check on your progress.
    Last edited by kbluhm; 01-11-2010 at 05:52 PM.

  • #7
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    Yes, I understand how to use file(). What I DON'T understand is how to get that if statement working correctly and such

  • #8
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    The if statement will return true if the coupon code is in the file because array_search will return the position of the code in the array so it would not be identical to false

  • #9
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    so should i use readfile instead of readlines() or what?

  • #10
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    How can I turn $myvar into an array of valid coupon codes?

    Here's what I have so far:

    PHP Code:
    <?php

    /*** make sure the file exists ***/
    $file 'my_file.txt';

    $myvar "b";
    $couponcode readLine($file2);


    // Get all lines of the file as an array, ignoring new line characters
    $lines file$fileFILE_IGNORE_NEW_LINES );

    // does the value of $myvar exist in the array?
    if ( FALSE !== ( $num array_search$myvar$lines ) ) )
    {
        
    // line number $num matched the value of $myvar

    echo $lines;

    }else{

    echo 
    "Lines that were read failed...";

    }  

    ?>
    Last edited by ShadowIce; 01-11-2010 at 06:34 PM.

  • #11
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by ShadowIce View Post
    Yes, I understand how to use file(). What I DON'T understand is how to get that if statement working correctly and such
    I'm not so sure that I agree with you. In your latest code you are calling file( $file ) before you check whether $file exists and is readable. Even in your original code, you were calling it with readFile(), which accesses the file, then you check if the file exists and is readable.

    Now if the file is always there, you would never even have know this flawed logic existed. Plus, if you always expect the file to be valid and readable, then there's no need for the file_exists() and is_readable() checks.




    Back to your original question, which didn't make a lot of sense. You have a file with a bunch of lines in it. These lines are coupon codes.

    You then have a variable that contains a coupon code as it's value. Your ultimate goal is to check the file to see if the coupon ode exists in the file.

    Is this correct?

    If so, will the coupon code exist more than once in the file? If so, you want to return 1) all line numbers that match, 2) the first line number that matches, or 3) just return TRUE or FALSE if it does or does not exist?

    Don't give us any code. Just explain in great detail what you want it to do and I'm sure we can help you out.
    Last edited by kbluhm; 01-11-2010 at 09:26 PM.

  • #12
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    um, dude? yea, i removed that if command...

    PHP Code:
    <?php

    /*** make sure the file exists ***/
    $file 'my_file.txt';

    $myvar "b";
    $couponcode readLine($file2);


    // Get all lines of the file as an array, ignoring new line characters
    $lines file$fileFILE_IGNORE_NEW_LINES );

    // does the value of $myvar exist in the array?
    if ( FALSE !== ( $num array_search$myvar$lines ) ) )
    {
        
    // line number $num matched the value of $myvar

    echo $lines;

    }else{

    echo 
    "Lines that were read failed...";

    }  

    ?>

  • #13
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Why are you echoing $lines? It is an array. You'll always see Array when you echo an array.

    Anyone else want to help this guy?

  • #14
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    ExCUSE me?! You know what fool! Just because I don't "automatically know the syntax in my head", doesn't mean you can't help me line-by-line so I know how to use it. I'm human too ya know, I'm willing to learn if someone is willing to teach me! So SCUUUUUUUUUUUUUUUUUUUUUUUSE me if I don't get it.... :@

  • #15
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    That's about enough of this worthless banter back and forth.

    kbluhm, if you can't or don't want to help ShadowIce any further, that's up to you. You're more than welcome to leave these topics for someone else. Thank you for your help so far.

    ShadowIce, you have got to be polite in your responses. You are asking for people to make the comments that they are making. To you and everyone else: do a simple Google search about your topic before asking for an example of something here. SI, everything you have asked so far has already been covered somewhere that is searchable by Google. When you ask for simple examples that you can find yourself, it simply wastes everyone else's time.

    So guys, please respect each other, do your own leg work before asking others to do it, and don't make me write another post like this.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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