Jump to content

[SOLVED] Find string in external file


lindm

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/75920-solved-find-string-in-external-file/
Share on other sites

<?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;
?>

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";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.