Page 1 of 1
How to break up text file?
Posted: Fri Aug 05, 2005 2:40 am
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
Posted: Fri Aug 05, 2005 3:36 am
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
Use
shuffle to get you the random index.
Posted: Fri Aug 05, 2005 3:44 am
by s.dot
if you want to do it by words
Code: Select all
$file = file_get_contents("textfile.txt");
$array[] = explode(" ",$file);
words
Posted: Fri Aug 05, 2005 4:16 am
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
Posted: Fri Aug 05, 2005 8:35 am
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);
?>
Re: words
Posted: Fri Aug 05, 2005 10:33 am
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.
Posted: Fri Aug 05, 2005 10:39 am
by feyd
file() is more efficient dumping a file into php broken apart by lines.
Trying to get code to work
Posted: Fri Aug 05, 2005 11:54 pm
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