Need some help.

Discussion in 'PHP' started by astrazone, Jul 22, 2009.

  1. #1
    <?php
    $handle = fopen("C:\file.txt", "r");
    if ($handle) {
    $buffer = fgets($handle);
    echo $buffer;
          if ($buffer == "Title: ") {
          echo "Success";
          }
    fclose($handle);
    }
    ?>
    PHP:
    the "echo $buffer" returns me "Title:"
    but its not entering the "if".:confused:

    Please Help me.
     
    astrazone, Jul 22, 2009 IP
  2. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #2
    Sorry my bad... too many coding languages makes me crazy :p
    $buffer == "Title: " ----> $buffer = "Title: "
    I need to use one "="

    sorry.
     
    astrazone, Jul 22, 2009 IP
  3. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #3
    Try
    
    if (trim($buffer).' ' == "Title: ") {
          echo "Success";
    }
    
    PHP:
     
    killaklown, Jul 22, 2009 IP
  4. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #4
    whats in the text file of = C:\file.txt ?

    does it say "Title" on line 1 .. ?
     
    ezprint2008, Jul 22, 2009 IP
  5. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #5
    content of file.txt
    I need to get Title,keywords,summary,and article body into vars.
     
    astrazone, Jul 23, 2009 IP
  6. ermac2014

    ermac2014 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i will do it for u...
    if u post some stuff in forums for me.. :)
     
    ermac2014, Jul 23, 2009 IP
  7. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #7
    right - first, read it all in 1 block of text.

    second. split it based upon new lines, eg $lines = split("\n", $buff);

    third. loop through each $lines and check against the sections you need, eg.

    for($ii = 0; $ii < count($lines); ++$ii) {
        if (stristr($lines[$ii], "Title:")) {
            $mydata['title'] = $lines[$ii+1];
        }
    
        etc etc.
    }
    
    print_r($mydata);
    
    PHP:
    good luck :) keep in mind that longer items like summary etc can be on more than 1 line so you need to loop until the next set of 2 empty line or something.
     
    dimitar christoff, Jul 23, 2009 IP
  8. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #8
    How can I use this with my file? I usually prefer not to use this system.
     
    astrazone, Jul 23, 2009 IP
  9. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #9
    your data source is not great so you need to 'template' it and write a parser - which is what I have started. i am sorry I don't have enough time to complete it atm - if you don't understand it then perhaps someone else will ...
     
    dimitar christoff, Jul 23, 2009 IP
  10. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #10
    If you simply want to know if Title: exists in a string you could do (only works on PHP5) :

    
    $pos = strpos($buffer, "Title:");
    /* !== and === are not typos, they're ways to check for true false, since == and != could interpret 0 as a false, when it may actually mean it found it at the beginning of the string */
    if($pos !== false)
    {
         /* "Title:" was found, do something */
    }
    
    PHP:
     
    kblessinggr, Jul 23, 2009 IP
  11. Puhal

    Puhal Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    uioouiouououiououo
     
    Puhal, Jul 23, 2009 IP
  12. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #12
    This is too true, I have to agree.
     
    wd_2k6, Jul 23, 2009 IP
  13. AlilG

    AlilG Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    the strpos function works in php4 too!
     
    AlilG, Jul 23, 2009 IP
  14. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Was wondering when someone was gona try to contest that.

    In PHP 4, strpos only matches a single character, in this instance it would return true for any instance of "T" found, but not specifically for the entire string. For that reason you have to use strstr() if using php4 and wanting to find a 'string' as opposed to a single character, but strstr is slower than strpos.

    http://www.php.net/manual/en/function.strpos.php
     
    kblessinggr, Jul 23, 2009 IP
  15. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #15
    er. why strpos and testing against it when you have strstr or stristr which work irregardless of pos as boolean? anyway, this is so besides the point...
    a parser for the whole file is needed. actually, a preg split may be possible here...
     
    dimitar christoff, Jul 23, 2009 IP
  16. AlilG

    AlilG Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    i see! ;) thanks
     
    AlilG, Jul 23, 2009 IP
  17. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #17
    best solution I found:
    $handle = @fopen("C:\file.txt", "r");
    $mode = "";
    $title = "";
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle);
    		if (trim($buffer) == "Title:") {
    			$mode = "title";
    			 continue;
    		}
    		if($mode == "title"){
    			$title = $title.$buffer;
    		}
        }
        fclose($handle);
    	
    }
    echo $title;
    
    PHP:
    thanks for all the help!
     
    astrazone, Jul 23, 2009 IP