Picking Out Specifics from a List

Discussion in 'PHP' started by hav0k, Oct 3, 2007.

  1. #1
    Alright, I got tons of help last time and now I'm stuck again...

    I have the following list:
    thumbs/thumb.Aaliyah-1.jpg
    thumbs/thumb.Aaliyah-22.jpg
    thumbs/thumb.Aaliyah-3.jpg
    thumbs/thumb.Abi-Titmuss-1.jpg
    thumbs/thumb.Abi-Titmuss-2.jpg
    thumbs/thumb.Abi-Titmuss-3.jpg
    thumbs/thumb.Ada-Nicodemou-6.jpg
    thumbs/thumb.Ada-Nicodemou-7.jpg
    thumbs/thumb.Adele-Silva-3.jpg
    thumbs/thumb.Adele-Silva-4.jpg
    thumbs/thumb.Adriana-Karembeu-1.jpg
    Code (markup):
    Using this script:
    <?php
    
        foreach (glob("thumbs/*.jpg") as $dirname) {
            echo "$dirname<br />";
        }
    
    ?>
    PHP:
    Now I need to modify the above script to make it so I can pull out only the files with "Aaliyah" in it. Any ideas on how to do this?
     
    hav0k, Oct 3, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    <?php
    
        foreach (preg_grep('/aaliyah/i', glob("thumbs/*.jpg")) as $dirname) {
            echo "$dirname<br />";
        }
    
    ?>
    
    PHP:
    Untested but should work.

    www.php.net/preg_grep


    EDIT:

    A better way could be using just glob though.
    
    <?php
    
        foreach (glob("thumbs/*aaliyha*.jpg") as $dirname) {
            echo "$dirname<br />";
        }
    
    ?>
    
    PHP:
    (But it's not case-insensitive)
     
    nico_swd, Oct 3, 2007 IP
  3. hav0k

    hav0k Active Member

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thanks, I didn't even think of doing that with "glob" but it worked.
     
    hav0k, Oct 3, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Just note that it might be case-sensitive on some systems. If it is, you can change all file names to lower case and it shouldn't be a problem anymore.
     
    nico_swd, Oct 3, 2007 IP