Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    428
    Thanks
    6
    Thanked 3 Times in 3 Posts

    how to get all files in folder apart from the ones that end with a 'p'

    I have this which does get all of the files but wish to know what method could I use to only get the files that do not have a 'p' at the end.
    L0123p.jpg (note the numbers are just for show, as my file names have letters and numbers)
    Code:
    $exist_imgs = glob('/home/username/public_html/itemImages/L*.jpg');

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,903
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Well, $exist is an array, so you can just filter it.

    PHP Code:
    foreach ($exist as $item)
    {
    If(
    basename($item) != *.*p// Do something !

    Untested, don't know if wildcard works in if statement.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    428
    Thanks
    6
    Thanked 3 Times in 3 Posts
    yes i would have used that method, but realised that i reuse the array again later on and needed only the files that did not have the 'p' at the end.

    well not that exact method, but something like it.
    Last edited by needsomehelp; 07-18-2013 at 04:22 PM.

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,903
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Quote Originally Posted by needsomehelp View Post
    yes i would have used that method, but realised that i reuse the array again later on and needed only the files that did not have the 'p' at the end.

    well not that exact method, but something like it.
    You could build an array in $_SESSION from that and use it later, anytime in session.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #5
    Regular Coder
    Join Date
    Oct 2009
    Posts
    428
    Thanks
    6
    Thanked 3 Times in 3 Posts
    no, sorry should have explained myself better.

    I wish to grab all images that do not have the 'p' at the end and use it first time around, then later on i use the same array again. both times i need only the images that do not have the 'p' at the end.

    i have been digging around and there is some suggestion about using preg match or something like that but no one has yet come up with an answer how to do it this way.

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Have a look at preg_grep():
    PHP Code:
    $exist_imgs glob'/home/username/public_html/itemImages/L*.jpg' );

    $exist_imgs_filtered preg_grep'/p\.jpg$/i'$exist_imgsPREG_GREP_INVERT ); 
    The PREG_GREP_INVERT flag tells it to return all the results that don't match the expression.
    Last edited by kbluhm; 07-19-2013 at 10:35 AM.

  • #7
    Regular Coder
    Join Date
    Oct 2009
    Posts
    428
    Thanks
    6
    Thanked 3 Times in 3 Posts
    that's the ticket, thank you.

  • #8
    New Coder
    Join Date
    Jun 2013
    Location
    The Republic of Texas
    Posts
    29
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Good call on the preg_grep. Untested, but without the invert:
    PHP Code:
    $exist_imgs_filtered preg_grep'/[^p]\.jpg$/i'$exist_imgs ); 

  • #9
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    I just want to pause this conversation to say that everytime I see

    $exist

    I read

    Sexist

    Thank you, and carry on.
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #10
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    Quote Originally Posted by needsomehelp View Post
    I have this which does get all of the files but wish to know what method could I use to only get the files that do not have a 'p' at the end.
    L0123p.jpg (note the numbers are just for show, as my file names have letters and numbers)
    Code:
    $exist_imgs = glob('/home/username/public_html/itemImages/L*.jpg');
    Try the following pattern:

    PHP Code:
    $exist_imgs glob('/home/username/public_html/itemImages/L*[^p].jpg'); 

  • #11
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by gvre View Post
    Try the following pattern:

    PHP Code:
    $exist_imgs glob('/home/username/public_html/itemImages/L*[^p]\.jpg'); 
    Did you even test that? It will not work, glob() is not PCRE compatible.
    Last edited by kbluhm; 07-20-2013 at 11:55 AM.

  • #12

  • #13
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by gvre View Post
    Yes, I tested the pattern and it worked. A better option is to use ! instead of ^ (php uses ! in the windows environment).
    Once again, that will not work. I'm not sure what you tested or how you tested it. glob() is not PCRE compatible. It uses decimals for directory traversing, asterisks for wildcards, and curly braces for grouping, all of which are PCRE special characters. That expression could be construed as an EREG pattern due to the lack of delimiters... but at any rate, it won't work.
    Last edited by kbluhm; 07-20-2013 at 04:55 PM.

  • #14

  • #15
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by gvre View Post
    Did you test it? I did and it worked.
    I tested it, and as expected it does not work. Are you messing with me? Mind posting the code that you magically got to work?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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