Read File Until Key Phrase is found

Hello,

I’m needing some help in figuring out how to create a script that will read a files content that would look for a certain key phrase to start at and another one to end at. The idea behind it is building a function that is similar to the Wordpress function but it would allow me to easily choose which section of a page to display in the “teaser text” rather than having to start at the top.

I really don’t want anyone to code it for me, just looking for a good advice on which functions to use and I should be able to go from there.

Thanks,
Anthony

instead of complicating things can’t you specify how many words or count the number of spaces and then insert the “teaser text”?

Hi valandor062,

See if this is close to what you are looking for:[php]$filename = ‘’;
$startText = ‘’;
$endText= ‘’;

$data= file_get_contents($filename);

$start = strpos($data,$startText);

if($start !== FALSE)
{
$end = strpos($data,$endText,$start)+strlen($endText);
if($end !== FALSE)
{
echo substr($data,$start,$end-$start);
}
}[/php]

It is set to include the full occurrence of the start text and end text in the output, but that could easily be changed. It sounds like your files will not be very large, so this method should work fine. If your files are large, I would probably modify the approach a little. Instead of reading the entire file in as a string, I would look at a line by line solution. The problem with this is that you would need to account for situations where the matching content is spread over multiple lines.

Let me know…

malasho- Thank you much for this! This will definitely work with a some tweaking. You rock!

liamsorsby- The reason I didn’t want to count spaces or characters is I’d like to be able to set a start and finish inside each posting for the teaser text. The teaser text wouldn’t always have the amount of spacing or characters/words in the paragraph I would want to show as teaser. It’s about making it look nice and not cutting it off in the middle of a word or in the middle of a sentence. Tho it may not seem like much in retrospect, I personally find it annoying when a site with a blog does this with it’s teaser, and one rule of thumb I use when trying to create something is “If I find it annoying then others may to, so I don’t do it.”

That and a little over half the people I’ve talked to on the subject find teaser text that cuts off in mid sentence as annoying. Tho they may click on it to read the last of the sentence they often times don’t finish reading the rest of the article.

So all in all, little annoyances can kill the chances of someone returning to your site and in today’s internet world repeat visitors are what will make or break you.

Thanks valandor062, I’m glad to hear it should help!

jay

So this is the code I’ve come up with so far. Still have a little bit to do, but just about there. Thanks again for the help! I read over the php.net site a lot but all because I’ve seen something doesn’t mean I can understand it until I can see it put into use.

[php]<?php
$dir = opendir(“Blog Posts”);
while (($file = readdir($dir)) !== FALSE)
{
if ($file != “.” && $file !="…")
{
$startText = ‘’;
$endText= ‘’;
$data= file_get_contents(‘Blog Posts/’ .$file);
$start = strpos($data,$startText);
if($start !=FALSE){
$end = strpos($data,$endText,$start)+strlen($endText);
if($end !=FALSE)
{
echo substr($data,$start,$end-$start);
}
}

  }

}

closedir($dir);

?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service