lindm Posted November 3, 2007 Share Posted November 3, 2007 Trying to build a small script which opens an external text file and looks for a string as below: Part of the text file where the string is below. THe string to be stored in a variable is THE STRING in the example below, this could however be any length: @page { @top-left{ -html2ps-html-content: "THE STRING<br/>1234 "; } @bottom-right{ -html2ps-html-content: counter(page)"("counter(pages)")"; } I believe the best approach is to identify the string is by defining it as being found between -html2ps-html-content: " AND html break code on the same row. Here is the beginning of my code. Need help with the fgets($h) part: $file = "file.txt"; $fh = fopen($file, 'r'); $string = fgets($fh); fclose($fh); echo $string; Thanks Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 3, 2007 Share Posted November 3, 2007 <?php $file = "file.txt"; $fh = fopen($file, 'r'); while($string = fgets($fh)){ $line = trim($string);//remove the line endings and extra spaces, gasp! list($check,$string) = explode(":",$line); if($check = "-html2ps-html-content"){echo $string;return true;} } fclose($fh); echo "No string found"; return false; ?> Quote Link to comment Share on other sites More sharing options...
Orio Posted November 3, 2007 Share Posted November 3, 2007 Doing exactly what you said would be simply this: <?php $contents = file_get_contents("filename.ext"); preg_match("/-html2ps-html-content: \"(.*?)<br\/>1234 \";/i", $contents, $match); echo $match[1]; ?> Orio. Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 3, 2007 Share Posted November 3, 2007 If there is more than one string to find... Use the below code: <?php $file = "file.txt"; $fh = fopen($file, 'r'); $i=0; while($string = fgets($fh)){ $line = trim($string);//remove the line endings and extra spaces, gasp! list($check,$string) = explode(":",$line); if($check = "-html2ps-html-content"){$holder[] = $string;$i++;} } fclose($fh); if($i){//there was at least one match print_r($holder); } else {//no matches echo "No string found"; } ?> Quote Link to comment Share on other sites More sharing options...
lindm Posted November 3, 2007 Author Share Posted November 3, 2007 Thanks for both your solutions. Marked as solved! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.