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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.