How to break up text file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nparekh
Forum Commoner
Posts: 31
Joined: Fri Aug 05, 2005 2:35 am

How to break up text file?

Post by nparekh »

Hi all

This is my first post here. I am brand new to php but have used it before on various parts of my website.

What I am looking for is some assistance in splitting a text file into "x" lines and then saving those in separate files.

What I require is then to include these separate files on different pages of my website (preferably randomly). The text is actually links to various pages on my website.

An example would be:

Lets say I have 100 words in a text file and want it to be broken down into 20 words per file. Then I would like to display those 20 words(which are weblinks) on a specific portion of my website. That way I would have 5 text files which would randomly be displayed on my site.

Any ideas if this can be done?

Thanks in advance

Nikhil
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Word ? As in 1 character ? Or a group of characters bounded by whitespaces ?

I dont see why you would want to break it in words or chars - if you have one link per line then you could do :

Code: Select all

$handle = fopen("links.txt","r");
while (!feof($handle))
 {
        $line = fgets($line);
 }
Or if you want all lines in an array then

Code: Select all

$line = file("links.txt");
Use shuffle to get you the random index.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

if you want to do it by words

Code: Select all

$file = file_get_contents("textfile.txt");
$array[] = explode(" ",$file);
nparekh
Forum Commoner
Posts: 31
Joined: Fri Aug 05, 2005 2:35 am

words

Post by nparekh »

Hi thanks for the replies.

Yes they are words e.g.

car
car rental
car insurance

so do I just add this code to a file and save as php?

Thanks

Nikhil
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

better "words" code, however not perfect, as that would be language specific..

Code: Select all

<?php

$words = preg_split('#\b(.+?)\b#', file_get_contents($filename), -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: words

Post by s.dot »

nparekh wrote:Hi thanks for the replies.

Yes they are words e.g.

car
car rental
car insurance

so do I just add this code to a file and save as php?

Thanks

Nikhil
it appears you want to split it by lines, which would not be good to split at the space because you'd split up your group of words.

Anjanesh's example should be good for that.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

file() is more efficient dumping a file into php broken apart by lines.
nparekh
Forum Commoner
Posts: 31
Joined: Fri Aug 05, 2005 2:35 am

Trying to get code to work

Post by nparekh »

Hi guys,

Thanks for all your replies.

I tried Anjanesh's code as follows:

<?php
$handle = fopen("keywords.txt","are");
while (!feof($handle))
{
$line = fgets($line);
}
?>

Saved into a file called break.php

I created a directory called logs and aved the break.php file and my keywords.txt file there.

keywords.txt contains data as follows:

car
car insurance
car rental
carjacking

etc - one word per line. It contains 200 keywords. I want to break it up into lots of "x" amount e.g 20 keywords each so would like to create 10 separate text files - how could I do it?

I tried Anjanesh's code but it doesnt seem to do anything - what am I doing wrong? The keyword file only contains 200 entries.

Is it possible to create 10 files as described above?

Thanks again


Nikhil
Post Reply