By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

Sort the output alphabetically

P: n/a
Hi,

how do I have to change/edit my code that the output is sort by the
file name (from a-z)?

<?php
$nr= 0;

$handle=opendir ($path);

while($datei = readdir($handle)) {
if (preg_match('/^w+.htm$/', $datei)) {

$filearray = file("$datei");
$filestring = implode("",$filearray);

echo "<tr>";
echo "<td class=\"text\" width=\"4%\">".++$nr."</td>";
echo "<td class=\"text\" width=\"32%\">
<a class=\"link\" href=\"$datei\"
target=\"_blank\">$datei</a></td>";

echo "<td class=\"verantw\" width=\"32%\">";
if (eregi ("!!!!(.*)!!!!", $filestring, $verantwortlicher)) echo
$verantwortlicher[1];
else echo '&nbsp;';
echo "</td>";

echo "<td class=\"text\" width=\"32%\">";
if (eregi ("<title>(.*)</title>", $filestring, $titel)) echo
$titel[1];
else echo '&nbsp;';
echo "</td>";

echo "</tr>";

}
}
?>

Thanks!
Jul 17 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
jagg wrote:
how do I have to change/edit my code that the output is sort by the
file name (from a-z)?

<?php
$nr= 0;

$handle=opendir ($path);
// initialize an array to hold the file names
$path_files = array();
while($datei = readdir($handle)) {
// add filename to the array
$path_files[] = $datei;
}
closedir($handle);

// sort the file names array
sort($path_files);

// loop with a foreach instead of the while() loop
foreach ($path_files as $datei) {
if (preg_match('/^w+.htm$/', $datei)) { (rest of script not checked and snipped)
Thanks!


You're welcome.

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #2

P: n/a
It works.... thank you very much!

But there is a problem with this code......

When there are more than ~150 files in a folder, the output stops
after ~150 files :-(! How can I make this code "faster"?
Jul 17 '05 #3

P: n/a
jagg wrote:
It works.... thank you very much!

But there is a problem with this code......

When there are more than ~150 files in a folder, the output stops
after ~150 files :-(! How can I make this code "faster"?


How can you have 150 files for which the name
matches /^w+.htm$/? ?!?!?!?!?!
<?php
$nr = 0;

$handle = opendir($path);

// initialize an array to hold the file names
$path_files = array();

while(($datei = readdir($handle)) !== false) {
// test file name *before* adding to array
if (preg_match('/^w+.htm$/', $datei)) {
// add filename to the array
$path_files[] = $datei;
}
}
closedir($handle);

// sort the file names array
sort($path_files);

// loop with a foreach instead of the while() loop
foreach ($path_files as $datei) {
// whatever, notice I removed the if()
}
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #4

P: n/a

"Pedro Graca" <he****@hotpop.com> schrieb im Newsbeitrag
news:sl*******************@ID-203069.user.uni-berlin.de...
How can you have 150 files for which the name
matches /^w+.htm$/? ?!?!?!?!?!


Hmm, there are 183 htm-files in that folder......!!!!

With your second code I get now ~164 htm-files then the output stops :-(

Another idea to get the last 19 ones!?
(...some files from these 19 have ~50k... so really big :-( )
Jul 17 '05 #5

P: n/a
jagg wrote:

"Pedro Graca" <he****@hotpop.com> schrieb im Newsbeitrag
news:sl*******************@ID-203069.user.uni-berlin.de...
How can you have 150 files for which the name
matches /^w+.htm$/? ?!?!?!?!?!
Hmm, there are 183 htm-files in that folder......!!!!


w.htm
ww.htm
wwwwwwwww.htm
w0htm
w1htm
wwwwxhtm

I can imagine 183 file names matching /^w+.htm$/, but I find it rather
awkward :)

With your second code I get now ~164 htm-files then the output stops :-(

Another idea to get the last 19 ones!?
(...some files from these 19 have ~50k... so really big :-( )

Do you *really* need to read all of the file just to get the
!!!!(.*)!!!! and <title>(.*)</title> ?

Maybe it's better to read each file line-by-line until you get the data
you need, especially if that data is near the top of the files.
Try clearing the variables before you use them:

unset($filearray, $filestring);
$filearray = file("$datei");
$filestring = implode("",$filearray);

If you have PHP >= 4.3.0 you can do

unset($filestring);
$filestring = file_get_contents($datei);

avoiding the use of $filearray
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #6

P: n/a

"Pedro Graca" <he****@hotpop.com> schrieb im Newsbeitrag
news:sl*******************@ID-203069.user.uni-berlin.de...
I can imagine 183 file names matching /^w+.htm$/, but I find it rather
awkward :)
hm, /^w+.htm$/ gets me ALL *.htm-files not only such files which begin with
a "w"!?!?
How would you do that?
unset($filestring);
$filestring = file_get_contents($datei);
Ok, done that.

Do you *really* need to read all of the file just to get the
!!!!(.*)!!!! and <title>(.*)</title> ?

Maybe it's better to read each file line-by-line until you get the data
you need, especially if that data is near the top of the files.

Yes, that is a good idea because the needed data are always near the
top of the file.
So how to read each file line-by-line until i get the needed data?

Thanks
Jul 17 '05 #7

P: n/a
jagg wrote:
"Pedro Graca" <he****@hotpop.com> schrieb im Newsbeitrag
news:sl*******************@ID-203069.user.uni-berlin.de... hm, /^w+.htm$/ gets me ALL *.htm-files not only such files which
begin with
a "w"!?!?
How would you do that?


echo 'w.htm is '; if (!preg_match('/^w+.htm$/', 'w.htm')) echo 'not ';
echo "matched.\n";
echo 'x.htm is '; if (!preg_match('/^w+.htm$/', 'x.htm')) echo 'not ';
echo "matched.\n";
echo 'ww.htm is '; if (!preg_match('/^w+.htm$/', 'ww.htm')) echo 'not ';
echo "matched.\n";
echo 'wx.htm is '; if (!preg_match('/^w+.htm$/', 'wx.htm')) echo 'not ';
echo "matched.\n";
echo 'wwwhtm is '; if (!preg_match('/^w+.htm$/', 'wwwhtm')) echo 'not ';
echo "matched.\n";
echo 'wwxhtm is '; if (!preg_match('/^w+.htm$/', 'wwxhtm')) echo 'not ';
echo "matched.\n";
unset($filestring);
$filestring = file_get_contents($datei);


Ok, done that.


Any change?
Do you *really* need to read all of the file just to get the
!!!!(.*)!!!! and <title>(.*)</title> ?

Maybe it's better to read each file line-by-line until you get the
data
you need, especially if that data is near the top of the files.

Yes, that is a good idea because the needed data are always near the
top of the file.
So how to read each file line-by-line until i get the needed data?

If !!!!(.*)!!!! and <title>(.*)</title> are always on one line, I might
try something along these lines:

foreach() {
fopen();
loop() {
fgets();
/* if both pieces of data found, end loop */
}
fclose();
}
Happy Coding :-)

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #8

P: n/a

"Pedro Graca" <he****@hotpop.com> schrieb im Newsbeitrag
news:sl*******************@ID-203069.user.uni-berlin.de...
echo 'w.htm is '; if (!preg_match('/^w+.htm$/', 'w.htm')) echo 'not ';
echo "matched.\n";
echo 'x.htm is '; if (!preg_match('/^w+.htm$/', 'x.htm')) echo 'not ';
echo "matched.\n";
echo 'ww.htm is '; if (!preg_match('/^w+.htm$/', 'ww.htm')) echo 'not ';
echo "matched.\n";
echo 'wx.htm is '; if (!preg_match('/^w+.htm$/', 'wx.htm')) echo 'not ';
echo "matched.\n";
echo 'wwwhtm is '; if (!preg_match('/^w+.htm$/', 'wwwhtm')) echo 'not ';
echo "matched.\n";
echo 'wwxhtm is '; if (!preg_match('/^w+.htm$/', 'wwxhtm')) echo 'not ';
echo "matched.\n";
Hm, you're right... BUT i get ALL htm-files with this preg_match!?! How is
this
possible? How must the preg_match looks like correct?
unset($filestring);
$filestring = file_get_contents($datei);


Ok, done that.


Any change?


Hm, I get now about 10 files more... but even not all ~183 files.
If !!!!(.*)!!!! and <title>(.*)</title> are always on one line, I might
try something along these lines:

foreach() {
fopen();
loop() {
fgets();
/* if both pieces of data found, end loop */
}
fclose();
}


Yes, they are.... So I play with this code. I hope I can find the rigt one
:-)!
Jul 17 '05 #9

P: n/a
jagg wrote:
Hm, you're right... BUT i get ALL htm-files with this preg_match!?! How is
this possible? How must the preg_match looks like correct?


Don't know ... something wrong with your code, perhaps?

Set the errorlevel in your script to report *everything*
Insert these two lines at the top, right after the opening "<?php"

ini_set('error_reporting', E_ALL);
ini_set('display_errors', '1');
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #10

P: n/a

"Pedro Graca" <he****@hotpop.com> schrieb im Newsbeitrag
news:sl*******************@ID-203069.user.uni-berlin.de...
ini_set('error_reporting', E_ALL);
ini_set('display_errors', '1');


I get NO errors when I put these two lines at the top of the code..... hm
Jul 17 '05 #11

P: n/a
jagg wrote:
How must the preg_match looks like correct?
if (preg_match('/^\w+\.htm$/', $filename)) { /* match found! */ }
# ________________^__^____

\w+ == one or more "word" characters
w+ == one or more "w"

\. == the dot itself
.. == any character (except newline, unless ...)

Hm, I get now about 10 files more... but even not all ~183 files.


What happens when you get 300 files?
Don't try to solve your problem for 200 files!

Solve it for any number of files!!
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #12

P: n/a

Ok, now it works perfect..... and with very high speed :-)

Changed the while to:
while($datei = readdir($handle))
{
if (($datei != ".") && ($datei != "..") && (!is_dir($datei)) &&
(preg_match('/^[\w-]+\.htm$/', $datei)))
{
$path_files[] = $datei;
}
}

and the
unset($filestring);
$filestring = file_get_contents($datei);


to:
$filestring = "";
$fp = fopen($datei, "r");
for ($i = 0; $i < 12; $i++)
{
$filestring .= fgets($fp, 4096);
}
fclose($fp);

=> so I only read the first 12 lines from every file....
Jul 17 '05 #13

P: n/a
jagg wrote:
Ok, now it works perfect..... and with very high speed :-)
Good!

Changed the while to:
while($datei = readdir($handle))
{
if (($datei != ".") && ($datei != "..") && (!is_dir($datei)) &&
(preg_match('/^[\w-]+\.htm$/', $datei)))
{
$path_files[] = $datei;
}
}
slight speed improvement:

if ((preg_match('/^[\w-]+\.htm$/', $datei)) && (!is_dir($datei)))
{
$path_files[] = $datei;
}

You don't need to test specifically for "." and "..", the preg_match
already does that.

I'm not so sure about this, but switching the order of the tests
and making php only test for is_dir() when the name matches
should make a difference too!

a) if ((preg_match()) && (!is_dir()))
b) if ((!is_dir()) && (preg_match()))

Try both ways and if the speed difference is noticeable
use the fastest :)

to:
$filestring = "";
$fp = fopen($datei, "r");
for ($i = 0; $i < 12; $i++)
{
$filestring .= fgets($fp, 4096);
}
fclose($fp);

=> so I only read the first 12 lines from every file....


ok ... but I was thinking of a (possibly) slower solution capable
of getting the data whether it is in the first 12 lines or not :-)

$excla_string = '';
$title_string = '';
$fp = fopen($datei, 'r');
while (!feof($fp)) {
$line = fgets($fp, 4096);
if (preg_match('/!!!!(.*)!!!!/', $line, $matches)) {
$excla_string = $matches[1];
}
if (preg_match('@<title>(.*)</title>@i', $line, $matches)) {
$title_string = $matches[1];
}
if ($excla_string && $title_string) break;
}
fclose($fp);
// now use $excla_string and $title_string

Happy Coding :-)
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #14

P: n/a
> a) if ((preg_match()) && (!is_dir()))
b) if ((!is_dir()) && (preg_match()))
b looks to be the faster one....
$excla_string = '';
$title_string = '';
$fp = fopen($datei, 'r');
while (!feof($fp)) {
$line = fgets($fp, 4096);
if (preg_match('/!!!!(.*)!!!!/', $line, $matches)) {
$excla_string = $matches[1];
}
if (preg_match('@<title>(.*)</title>@i', $line, $matches)) {
$title_string = $matches[1];
}
if ($excla_string && $title_string) break;
}
fclose($fp);


Use your code now.... it seems that this is the faster one too
(most !!!!- and title-lines are before the 12th line..)

Again THANKS for your help!!!
Jul 17 '05 #15

P: n/a
Hi Pedro,

can I ask you what I have to change in the (now) existing code that I can
addtional sort the output alphabeticall BY THE $excla_string
(=$verantwortlicher)?
---------------------------------------------
$nr= 0;

$handle=opendir ($path);
$path_files = array();

while($datei = readdir($handle))
{
if ((!is_dir($datei)) && (preg_match('/^[\w-]+\.htm$/', $datei)))
{
$path_files[] = $datei;
}
}

closedir($handle);
sort($path_files);

foreach ($path_files as $datei) {

$verantwortlicher = '';
$title = '';
$fp = fopen($datei, 'r');
while (!feof($fp)) {
$line = fgets($fp, 4096);
if (preg_match('/!!!!(.*)!!!!/', $line, $matches)) {
$verantwortlicher = $matches[1];
}
if (preg_match('@<title>(.*)</title>@i', $line, $matches)) {
$title = $matches[1];
}
if ($verantwortlicher && $title) break;
}
fclose($fp);

echo "<tr>";
echo "<td class=\"text\" width=\"4%\">".++$nr."</td>";
echo "<td class=\"text\" width=\"28%\"><a class=\"link\" href=\"$datei\"
target=\"_blank\">$datei</a></td>";

echo "<td class=\"verantw\" width=\"25%\">";
if ($verantwortlicher) echo $verantwortlicher;
else echo '&nbsp;';
echo "</td>";

echo "<td class=\"text\" width=\"35%\">";
if ($title) echo $title;
else echo '&nbsp;';
echo "</td>";
}
-----------------------------

Thank you very much.

Regards,
jagg
Jul 17 '05 #16

P: n/a
jagg wrote:
Hi Pedro,

can I ask you what I have to change in the (now) existing code that I can
addtional sort the output alphabeticall BY THE $excla_string
(=$verantwortlicher)?
I like $excla_string better LOLOLOL
---------------------------------------------


As you're sorting by $verantwortlicher (tough typing), there's no need
for the sort($path_files). Before sorting you need all the data, so
those echo's must be moved outside the loop and the data saved (to an
array). When the loop is finished, sort the array and print it
.... but ...


Maybe it's time to refactor your code.
I do not do OOP, maybe you'd like to do it differently ...
[Not really thought about the following ... maybe it's a good start]

Make everything you showed us a stand-alone function, which in turn
calls other functions.

function show_data_from_files($path) {
$path_files = get_files($path); // array with file names
foreach ($path_files as $datei) {
$file_data[$datei] = get_file_data($datei); // array with !!!! and title
}
uasort($file_data, 'cmp_file_data'); // compare on !!!!
print_data($file_data); // your echo's go inside this function
}


--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.