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
How to break up text file?
Moderator: General Moderators
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 :
Or if you want all lines in an array then
Use shuffle to get you the random index.
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);
}Code: Select all
$line = file("links.txt");if you want to do it by words
Code: Select all
$file = file_get_contents("textfile.txt");
$array[] = explode(" ",$file);- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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
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.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
Anjanesh's example should be good for that.
Trying to get code to work
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
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