Reading a text file in sections at a time

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

flash_harry
New php-forum User
New php-forum User
Posts: 26
Joined: Thu Jan 16, 2003 6:05 am

Reading a text file in sections at a time

Postby flash_harry » Wed May 07, 2003 6:12 am

I want to read the text out of a file in sections. Then print it to my page. However I need to read it out in sections which don't cut off half way through a word so fread won't work and the text file is all on one line so fgets won't work as it brings in the whole file. Anyone any ideas.
Much appreciated.

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Wed May 07, 2003 6:14 am

What's the file contents look like?

Andrew

flash_harry
New php-forum User
New php-forum User
Posts: 26
Joined: Thu Jan 16, 2003 6:05 am

Postby flash_harry » Wed May 07, 2003 7:03 am

It's a single line of text made up of lots of sentences.

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Wed May 07, 2003 7:45 am

ok, and do these sentences change?

Andrew

flash_harry
New php-forum User
New php-forum User
Posts: 26
Joined: Thu Jan 16, 2003 6:05 am

Postby flash_harry » Wed May 07, 2003 8:36 am

As in change length and structure? Yes. Here is a sample, and remember this is all on one line.

It was a warm sunny day when Paul went out for a walk. He met up with Andy and they went into town to do some shopping. The story countinues on in this manner and has no real significance.

I need it to print out sections like these from the same text file

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Wed May 07, 2003 9:14 am

hmmmmm, I could read it all out as one variable, but splitting it I'm not 100% sure about...

flash_harry
New php-forum User
New php-forum User
Posts: 26
Joined: Thu Jan 16, 2003 6:05 am

Postby flash_harry » Wed May 07, 2003 11:56 pm

Yeah, I read it all out as one variable but, I really need it out in sections or sentences.
Can anyone help???

User avatar
Skeletor
New php-forum User
New php-forum User
Posts: 147
Joined: Tue Nov 19, 2002 7:47 am
Location: US, Ohio
Contact:

Postby Skeletor » Thu May 08, 2003 5:24 am

Is it possible for you to put a delimiter between the sentences/phrases? If not, do they all end with punctuation followed by a space and a capitalized alphanumeric character? You need something common between the sentences so the program can split them apart.

you could then use a string function to split up the text and print it out. Such as: strtok(). I think a function like this will allow you to parse up your string, but you need a common delimiter.

Good luck

flash_harry
New php-forum User
New php-forum User
Posts: 26
Joined: Thu Jan 16, 2003 6:05 am

Postby flash_harry » Thu May 08, 2003 5:39 am

Yeah, I got it sorted just before I read your post there Skeletor. Just incase either of you or anyone else was wondering, here's how i actually did it.

Code: Select all

<?php
$delimiter = ".";

$ftr = fopen("test.txt","r") or die("No File");
while(!feof($ftr)){
   $all = fgets($ftr,10000);
}
          
do{
   $sentence = strtok($all, $delimiter);
   $all = str_replace($sentence,"",$all);
   print "$sentence.";
}while($sentence!="");
?>

User avatar
Skeletor
New php-forum User
New php-forum User
Posts: 147
Joined: Tue Nov 19, 2002 7:47 am
Location: US, Ohio
Contact:

Postby Skeletor » Thu May 08, 2003 8:47 am

Good deal. I was on the same wavelength and thought of something similar. The only problem I was worried about with using a "." as the delimiter was that if it appeared for any reason in the middle of a sentence. If it did, it would parse up the string wrong. I wonder if you could use an ereg function/string and create a delimiter that looks for a period, a space, and then a capital letter. This might better indicate which is an actual end of a sentence, and just a period used within a sentence.

Just a suggestion :]

Good luck

flash_harry
New php-forum User
New php-forum User
Posts: 26
Joined: Thu Jan 16, 2003 6:05 am

Postby flash_harry » Fri May 09, 2003 3:20 am

While we're at it. This is the bit of code i've been producing.

Code: Select all

<?php
$count = 0;
$delimiter = ".";
$images = array();
while(!feof($ftr)){
  $all = fgets($ftr,10000);
}
          
do{
  $sentence = strtok($all, $delimiter);
  $all = str_replace($sentence,"",$all);
  print "$sentence. ";
  if($count ==10){
    $image = getRandomImage('./images/');
    for($i=0; $i<(sizeof($images)); $i++){
      if(stristr($image,$images[$i])){
        while(stristr($image,$images[$i])){
          $image = getRandomImage('./images/');
        }
      }
      print("$images[$i]");
    }

    echo "<img src='$image' align=left><br>";
    $images[] = $image;
    $count = -10;
  }
  $count++;
}while($sentence!="");
?>


Now I have the problem that the random pictures i have been inputing can end up being the same a number of times down the page ie. randomly selected twice or more. This code doesn't seem to stop that happening. Any ideas why?


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron