Alphabetical text file sorting
Moderator: General Moderators
-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
Alphabetical text file sorting
Hi,
I need to add strings of text to a text file in alphabetical order. I'll provide a form for a user to input the text then once submitted the string will be inserted into the file in order.
Each line with have only one string, so 10 strings will mean 10 lines.
Are there any functions for sorting aplhabetically? Any suggestion would be appreciated.
Thanks,
Rob.
I need to add strings of text to a text file in alphabetical order. I'll provide a form for a user to input the text then once submitted the string will be inserted into the file in order.
Each line with have only one string, so 10 strings will mean 10 lines.
Are there any functions for sorting aplhabetically? Any suggestion would be appreciated.
Thanks,
Rob.
sort() should do it.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
make sure that you turn it into a array first though because sort() wont work on a string. so if there is a text field where the person inputs words seperated by a space do somtin like
Code: Select all
$words = $_POST['words'];
$words = trim($words);
$words = explode(' ', $words);
$words = sort($words);
//$words is now alphabetically sorted, to write to a txt file...
$open = fopen('file.txt', 'a');//note, this will make file length 0, check out fopen in the manual
foreach($words as $val)
{
fwrite($val, $open);
}
fclose($open);-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
Hi,
I have tried a few things but with little joy.
I am reading the contents of a text file to an array:
Then I am adding the new input text to the last index of the array:
Then sorting the array:
Then testing the output:
However when I add the string to the last index it doesn't seem to write to the array and then after sorting all the indexes seem to be empty.
I don't think I am far of solving this, any ideas guys?
Thanks,
Rob.
I have tried a few things but with little joy.
I am reading the contents of a text file to an array:
Code: Select all
$list=file("email_list.txt");Code: Select all
$x=count($list);
$x++;
$list[$x]=test_string;Code: Select all
$list=sort($list);Code: Select all
for($y=0; $y<$x; $y++){
echo"$y: $list[$y]<br>";
}I don't think I am far of solving this, any ideas guys?
Thanks,
Rob.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
take out the post-icrement line, it's not needed.
will append to the array.
Code: Select all
$list[] = 'some string';-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
Ok this is very odd, I have stripped the code down to bare basics, it seems that the sort function is deleting the array index values:
If you run the code, before sorting the data is output, after sorting nothing is output!
Any ideas?
Thanks,
Rob.
Code: Select all
<?
$list=file("email_list.txt");
$x=count($list);
for($y=0; $y<$x; $y++){
echo"$y: $list[$y]<br>";
}
$list=sort($list);
for($y=0; $y<$x; $y++){
echo"$y: $list[$y]<br>";
}
?>Any ideas?
Thanks,
Rob.
-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
You can see the output here:
http://www.robburne.f2s.com/email/
Although this doesn't help just confirms what I am saying, the osrting seems to be deleting any data in the array indexes.
How can I sort the array and keep the data contained within.
Thanks,
Rob.
http://www.robburne.f2s.com/email/
Although this doesn't help just confirms what I am saying, the osrting seems to be deleting any data in the array indexes.
How can I sort the array and keep the data contained within.
Thanks,
Rob.
This should work:
Kind of off topic, but look into foreach loops - they're much nicer than using the for loop you had.
Code: Select all
<?
$list = file("email_list.txt");
//output before sort
foreach($list as $index=>$value)
{
echo "$index: $value<br />";
}
//add your new entries here
sort($list);
//output after sort
foreach($list as $index=>$value)
{
echo "$index: $value<br />";
}
?>Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
This is possible the most frustrating piece of code I have ever worked on.
This is where I am at so far, an old file is opened for read/write. The file contains 3 lines - z,m and a. A variable $email is written to the end of the file. So if say the variable was 'c' the old file now reads as follows in notepad.
z
m
a
c
All good so, now onto sorting.
Now onto writting the sorted array to a new file:
All appears to be ok , but when I look at the contents of the file I have:
a
cm
z
The order is correct but as you can see there is no carriage return after the inserted variable. It seems as if I somehow have to write a carriage return to the end of the file after adding the last line. I have tried with \n \r etc but all that seems to do is write a odd square charater to the file.
I am sure this is something simple, any idea please guys?
Thanks,
Rob.
This is where I am at so far, an old file is opened for read/write. The file contains 3 lines - z,m and a. A variable $email is written to the end of the file. So if say the variable was 'c' the old file now reads as follows in notepad.
z
m
a
c
Code: Select all
$handle=fopen("email_list.txt","a+");
fwrite($handle, "$email");
fclose($handle);Code: Select all
$list=file("email_list.txt");
sort($list);Code: Select all
$handle2=fopen("email_list2.txt","x+");
foreach($list as $val)
{
fwrite($handle2, $val, strlen($val));
}
fclose($handle2);a
cm
z
The order is correct but as you can see there is no carriage return after the inserted variable. It seems as if I somehow have to write a carriage return to the end of the file after adding the last line. I have tried with \n \r etc but all that seems to do is write a odd square charater to the file.
I am sure this is something simple, any idea please guys?
Thanks,
Rob.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
//.......
// load the file, strip out surrounding whitespace and remove any blank or newly blank lines
$list = array_filter(array_map('trim',file($filename),'strlen');
$data = implode("\r\n", $list);
$fp = fopen($newFilename,'w');
fwrite($fp, $data);
fclose($fp);