Sort the output alphabetically

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jagg

    Sort the output alphabetically

    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("",$fil earray);

    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, $verantwortlich er)) echo
    $verantwortlich er[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!
  • Pedro Graca

    #2
    Re: Sort the output alphabetically

    jagg wrote:[color=blue]
    > 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);[/color]

    // initialize an array to hold the file names
    $path_files = array();
    [color=blue]
    > while($datei = readdir($handle )) {[/color]

    // add filename to the array
    $path_files[] = $datei;
    }
    closedir($handl e);

    // sort the file names array
    sort($path_file s);

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

    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. |

    Comment

    • jagg

      #3
      Re: Sort the output alphabetically

      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"?

      Comment

      • Pedro Graca

        #4
        Re: Sort the output alphabetically

        jagg wrote:[color=blue]
        > 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"?[/color]

        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($handl e);

        // sort the file names array
        sort($path_file s);

        // 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. |

        Comment

        • jagg

          #5
          Re: Sort the output alphabetically


          "Pedro Graca" <hexkid@hotpop. com> schrieb im Newsbeitrag
          news:slrnceksau .16c.hexkid@ID-203069.user.uni-berlin.de...[color=blue]
          > How can you have 150 files for which the name
          > matches /^w+.htm$/? ?!?!?!?!?![/color]

          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 :-( )


          Comment

          • Pedro Graca

            #6
            Re: Sort the output alphabetically

            jagg wrote:[color=blue]
            >
            > "Pedro Graca" <hexkid@hotpop. com> schrieb im Newsbeitrag
            > news:slrnceksau .16c.hexkid@ID-203069.user.uni-berlin.de...[color=green]
            >> How can you have 150 files for which the name
            >> matches /^w+.htm$/? ?!?!?!?!?![/color]
            >
            > Hmm, there are 183 htm-files in that folder......!!! ![/color]

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

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

            [color=blue]
            > 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 :-( )[/color]


            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($filearra y, $filestring);
            $filearray = file("$datei");
            $filestring = implode("",$fil earray);

            If you have PHP >= 4.3.0 you can do

            unset($filestri ng);
            $filestring = file_get_conten ts($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. |

            Comment

            • jagg

              #7
              Re: Sort the output alphabetically


              "Pedro Graca" <hexkid@hotpop. com> schrieb im Newsbeitrag
              news:slrncel52h .16c.hexkid@ID-203069.user.uni-berlin.de...[color=blue]
              > I can imagine 183 file names matching /^w+.htm$/, but I find it rather
              > awkward :)[/color]

              hm, /^w+.htm$/ gets me ALL *.htm-files not only such files which begin with
              a "w"!?!?
              How would you do that?
              [color=blue]
              > unset($filestri ng);
              > $filestring = file_get_conten ts($datei);[/color]

              Ok, done that.

              [color=blue]
              > 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.[/color]


              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


              Comment

              • Pedro Graca

                #8
                Re: Sort the output alphabetically

                jagg wrote:[color=blue]
                > "Pedro Graca" <hexkid@hotpop. com> schrieb im Newsbeitrag
                > news:slrncel52h .16c.hexkid@ID-203069.user.uni-berlin.de...[/color]
                [color=blue]
                > hm, /^w+.htm$/ gets me ALL *.htm-files not only such files which
                > begin with
                > a "w"!?!?
                > How would you do that?[/color]

                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 ";
                [color=blue][color=green]
                >> unset($filestri ng);
                >> $filestring = file_get_conten ts($datei);[/color]
                >
                > Ok, done that.[/color]

                Any change?
                [color=blue][color=green]
                >> 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.[/color]
                >
                >
                > 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?[/color]


                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. |

                Comment

                • jagg

                  #9
                  Re: Sort the output alphabetically


                  "Pedro Graca" <hexkid@hotpop. com> schrieb im Newsbeitrag
                  news:slrncelcbc .16c.hexkid@ID-203069.user.uni-berlin.de...[color=blue]
                  > 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 ";[/color]

                  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?
                  [color=blue][color=green][color=darkred]
                  > >> unset($filestri ng);
                  > >> $filestring = file_get_conten ts($datei);[/color]
                  > >
                  > > Ok, done that.[/color]
                  >
                  > Any change?[/color]

                  Hm, I get now about 10 files more... but even not all ~183 files.
                  [color=blue]
                  > 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();
                  >}[/color]

                  Yes, they are.... So I play with this code. I hope I can find the rigt one
                  :-)!


                  Comment

                  • Pedro Graca

                    #10
                    Re: Sort the output alphabetically

                    jagg wrote:[color=blue]
                    > 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?[/color]

                    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('displa y_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. |

                    Comment

                    • jagg

                      #11
                      Re: Sort the output alphabetically


                      "Pedro Graca" <hexkid@hotpop. com> schrieb im Newsbeitrag
                      news:slrnceldkd .16c.hexkid@ID-203069.user.uni-berlin.de...[color=blue]
                      > ini_set('error_ reporting', E_ALL);
                      > ini_set('displa y_errors', '1');[/color]

                      I get NO errors when I put these two lines at the top of the code..... hm


                      Comment

                      • Pedro Graca

                        #12
                        Re: Sort the output alphabetically

                        jagg wrote:[color=blue]
                        > How must the preg_match looks like correct?[/color]

                        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 ...)

                        [color=blue]
                        > Hm, I get now about 10 files more... but even not all ~183 files.[/color]

                        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. |

                        Comment

                        • jagg

                          #13
                          Re: Sort the output alphabetically


                          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[color=blue][color=green][color=darkred]
                          > >> unset($filestri ng);
                          > >> $filestring = file_get_conten ts($datei);[/color][/color][/color]

                          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....


                          Comment

                          • Pedro Graca

                            #14
                            Re: Sort the output alphabetically

                            jagg wrote:[color=blue]
                            > Ok, now it works perfect..... and with very high speed :-)[/color]

                            Good!

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

                            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 :)

                            [color=blue]
                            > 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....[/color]

                            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. |

                            Comment

                            • jagg

                              #15
                              Re: Sort the output alphabetically

                              > a) if ((preg_match()) && (!is_dir()))[color=blue]
                              > b) if ((!is_dir()) && (preg_match()))[/color]

                              b looks to be the faster one....
                              [color=blue]
                              > $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);[/color]

                              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!!!


                              Comment

                              Working...