Page 1 of 1
Alphabetical text file sorting
Posted: Thu Aug 04, 2005 10:03 am
by spacebiscuit
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.
Posted: Thu Aug 04, 2005 10:06 am
by pickle
Posted: Thu Aug 04, 2005 10:38 am
by shiznatix
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);
Posted: Sun Aug 07, 2005 3:53 pm
by spacebiscuit
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:
Code: Select all
$x=count($list);
$x++;
$list[$x]=test_string;
Then sorting the array:
Then testing the output:
Code: Select all
for($y=0; $y<$x; $y++){
echo"$y: $list[$y]<br>";
}
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.
Posted: Sun Aug 07, 2005 4:22 pm
by feyd
take out the post-icrement line, it's not needed.
will append to the array.
Posted: Mon Aug 08, 2005 7:42 am
by spacebiscuit
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:
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>";
}
?>
If you run the code, before sorting the data is output, after sorting nothing is output!
Any ideas?
Thanks,
Rob.
Posted: Mon Aug 08, 2005 7:44 am
by feyd
the array is re-indexed when sort() runs.. if the indexes are off, they will be rearranged to be within alignment (sequencial)
Posted: Mon Aug 08, 2005 8:22 am
by spacebiscuit
Ok so what does that mean and how does it apply to my case?
Thanks,
Rob.
Posted: Mon Aug 08, 2005 8:57 am
by feyd
print_r() the array before and after, you'll see..
Posted: Mon Aug 08, 2005 9:39 am
by spacebiscuit
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.
Posted: Mon Aug 08, 2005 10:06 am
by feyd
oops.. I forgot that sort() does not return the new array. It directly modifies the existing one. So don't assign the return from sort() to anything.
Posted: Mon Aug 08, 2005 10:28 am
by pickle
This should work:
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 />";
}
?>
Kind of off topic, but look into
foreach loops - they're much nicer than using the for loop you had.
Posted: Mon Aug 15, 2005 5:01 pm
by spacebiscuit
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
Code: Select all
$handle=fopen("email_list.txt","a+");
fwrite($handle, "$email");
fclose($handle);
All good so, now onto sorting.
Code: Select all
$list=file("email_list.txt");
sort($list);
Now onto writting the sorted array to a new file:
Code: Select all
$handle2=fopen("email_list2.txt","x+");
foreach($list as $val)
{
fwrite($handle2, $val, strlen($val));
}
fclose($handle2);
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.
Posted: Mon Aug 15, 2005 5:11 pm
by feyd
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);
adapt as needed..