<?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". Please Help me.
Sorry my bad... too many coding languages makes me crazy $buffer == "Title: " ----> $buffer = "Title: " I need to use one "=" sorry.
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.
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 ...
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:
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
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...
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!