SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2000
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a file with about 700 lines, I need to sort each of these lines in alphabetical order.. so I figured it would be easy with PHP

    Right now i'm using this code:

    <?

    $array=file("/home/reboot/list.txt");

    sort($array);

    for ($counter=0; $counter<=count($array); $counter++)
    {
    print($array[$counter]);
    print("<BR><BR>");
    }

    print("done");


    ?>

    But the problem with that is it will sort capital and non-capital words differently.. Is there a way I could make php ignore the fact that a word is capital or not?

    Thanks,

    -Matt

    ------------------
    -----
    ComicalPics.com

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You need to use usort(), a function that allows you to set your own criteria for what order to sort array elements into, to compare elements based on their order ignoring case:

    function alpha_comp($a, $b) {
    $al = strtolower($a);
    $bl = strtolower($b);
    if ($al == $bl) return 0;
    return ($al < $bl) ? -1 : 1;
    }

    usort($array, alpha_comp);


    ------------------
    -Kevin Yank.
    http://www.SitePoint.com/
    Helping Small Business Grow Online!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •