SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Comparing a statment to a text file?

    Hey guys,

    I ran into a small problem and was wondering if something like this was possible...

    A user inputs a symbol into a textbox ( Ex. MSFT )

    Now is there anyway to compare what was inputed into that text box with a text file to see if that combination of characters exists within the actual text file?

    An example of this text file would be :

    http://www.nasdaqtrader.com/dynamic/SymDir/otclist.txt

    I'm just in my learning stage of PHP but was wondering if something like this is possible?

    Any help would be appreciated.

    Thanks,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Of course, if you added that file to a database it would be a matter of simple sql query.

    But to answer your question, here's one way:

    Code php:
    $symbol='MSFT';
     
    $fp = fopen('otclist.txt','r');
     
    while($row=fgetcsv($fp,null,'|')){
      if(!empty($row[0]) && $row[0]==$symbol) break;
    }
     
    fclose($fp);
     
    print_r($row);

  3. #3
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Thank you for the reply.

    I tried the code in a separate file to see if it works, but it outputs a blank page.

    Can anybody think of a reason why I may be getting a blank page?

    Meanwhile I'm going to keep trying to get it to work.

    Thank,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Add to the top of the script:

    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 

  5. #5
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Thak you for taking the time to answer my question...

    I added the code to the top of the page, unfortunately it's still blank.

    The test page can be found here:

    http://www.resourcebubble.com/site/w...fault/test.php

    The exact code that's on the page is:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <?php
    
    ini_set('display_errors', 1);
    
    error_reporting(E_ALL); 
    
    $symbol='Security';
     
    $fp = fopen('otclist.txt','r');
     
    while($row=fgetcsv($fp,null,'|')){
      if(!empty($row[0]) && $row[0]==$symbol) break;
    }
     
    fclose($fp);
     
    print_r($row);
    
    ?>
    
    </body>
    </html>
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  6. #6
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    <pre>
    <?php

    $symbol
    ='MSFT';
     
    $fp fopen('otclist.txt','r');
     
    while(
    $row[]=fgetcsv($fp,null,'|'))
    {
        if(!empty(
    $row[0]) && $row[0]==$symbol) break;
    }
     
    fclose($fp);
     
    print_r($row);
    my mobile portal
    ghiris.ro

  7. #7
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the reply Ernie1.

    With your code this shows:

    http://www.resourcebubble.com/site/w...fault/test.php

    But unfortunately nothing is pulled from the text file. I even made sure to change $symbol to something that exists int he text file.

    Any other suggestions?

    I just need a simple true or false if $symbol exists in the text file.

    Thanks,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  8. #8
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have saved the file to my local server and it displays.
    my mobile portal
    ghiris.ro

  9. #9
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Ernie1 can you link me to the file and just paste the exact code you used?

    I must be missing something. Maybe something needs to be enables in the php.ini file? I'm hosted on Media Temple.

    Thanks,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  10. #10
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you change

    $row=fgetcsv($fp,null,'|')

    to

    $row[]=fgetcsv($fp,null,'|')

    then

    if(!empty($row[0]) && $row[0]==$symbol)

    is no longer correct

  11. #11
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess it's harder to do then it seems. I will look online for tutorials on this.

    But if anybody can get something like this working please reply.

    Thanks you for the help php_daemon & Ernie1 maybe we can fix the code somehow. I will keep trying.

    Thanks,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You say you want to search for a symbol, yet the code you posted is trying to search for 'Security'.

    The code given to you will only match the first part of each line up to the pipe character |
    It will only match case sensitive. The entire string must match, no partial matching will occur.

    For example, searching for
    AAA or aaae

    will not match the AAAE symbol.

  13. #13
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    crmalibu I've tried changing $security to CGFX, PCPE, and even PCWRQ which all exist before the pipe characted at the beginning of a line.

    However this also resulted in a blank screen. Any clue as to why that is happening?

    Did the small script yield a correct output for anybody out there?

    Thank you for your time guys,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  14. #14
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the output of this?
    PHP Code:
    var_dump(filesize('otclist.txt')); 

  15. #15
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    What is the output of this?
    PHP Code:
    var_dump(filesize('otclist.txt')); 
    int(3779686)
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  16. #16
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make a new file with nothing in it but this. Post the output back here in [code] tags.

    PHP Code:
    <?php

    ini_set
    ('display_errors'1);
    error_reporting(E_ALL); 
    header('content-type: text/plain');
     
    $fp fopen('otclist.txt','r');

    $i=0;
    while(
    $row=fgetcsv($fp,null,'|')){
      if (
    $i++ > 5) break;
      
    var_dump($row);
    }

  17. #17
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the exact page with that code in it:

    http://www.resourcebubble.com/site/w...fault/test.php

    The output is blank, any reason for this?

    Thank you,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  18. #18
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    new file, post output
    PHP Code:
    echo base64_encode(substr(file_get_contents('otclist.txt'), 0300)); 

  19. #19
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    U3ltYm9sfFNlY3VyaXR5IE5hbWV8TWFya2V0IENhdGVnb3J5fFN0YXR1c3xUZXN0IElzc3VlCkFBQUV8QUFBIEVuZXJneSBJbmMuIENvbW1vbiBTdG9ja3xCQnxBQ1RJVkV8CkFBQUZGfEFkb2xmIEFhaGxlcnMgQSBHIE9yZGluYXJ5IFNoYXJlc3xOQkJ8fApBQUFHWXxBbHRhbmEgUGxjIEFkcyBBbWVyaWNhbiBEZXBvc2l0YXJ5IFNoYXJlc3xOQkJ8fApBQUFJRnxBbWVyaWNhbiBBcXVhdGVjaCBJbnRsIEluYyAoRikgT3JkaW5hcnkgU2hhcmVzfE5CQnx8CkFBQUxGfEFhcmVhbCBCYW5rIEFHIEFLVHxOQkJ8fApBQUFPRnxBQUEg
    Link: http://www.resourcebubble.com/site/w...ault/test1.php
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  20. #20
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The output is?
    PHP Code:
    ini_set('display_errors'1); 
    error_reporting(E_ALL); 
    header('content-type: text/plain'); 

    $fp fopen('otclist.txt','r'); 
    var_dump($fp);
    var_dump(fgetcsv($fpnull'|'));
    var_dump(fgetcsv($fp500'|')); 

  21. #21
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  22. #22
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    php_daemons's origional code should work. Just change null to 1000

  23. #23
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Amazing, it's weird how much 1 number can change.

    Now it's time to go over the code and figure out exactly what was done there.

    Thank you for the help guys, I really appreciate you taking the time to help me with my problem.

    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  24. #24
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ha, and there I thought null is equivalent to omitting a parameter, live and learn.


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
  •