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.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2010
    Location
    Chicago, Illinois
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Syntax for Wildcard in String?

    I have a script that looks through a directory and displays files with either the extension jpg or gif. I want to add to that criteria that it should not display any file with a name that begins with "ref_".

    I am not getting any results using * as a wildcard replacement for the rest of the file name. If I replace the wildcard * with a precise filename, the script works fine.

    Can anyone advise as to what is wrong with my syntax? It looks just like dozens of examples I've seen used elsewhere, including a few on this forum. So I'm rather puzzled.

    Thank you for any help you can provide.

    Elizabeth

    PHP Code:
      // open the directory
      
    $dir opendir$pathToImages );

        
    // parse path
        
    $info pathinfo($pathToImages $fname); 
          
        
    //strips extension off file name for later use
        //assumes file extension is three characters
            
    $pos strrpos($fname'.');
            
    $basename substr($fname0$pos);
            
    $extension substr($fname$pos+1);   
         
        
    // strip the . and .. entries off and parse for valid jpg or gif
        
    if ($basename != 'ref_' "*"  && (strtolower($info['extension']) == 'jpg') || (strtolower($info['extension']) == 'gif')) 
    Last edited by bronzino; 11-16-2010 at 03:48 AM.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Change:

    Code:
    if ($basename != 'ref_' . "*"  &&
    to:

    Code:
    if (strpos($basename, 'ref_') === false  &&
    That will match if ref_ is anywhere in the filename. To limit it to the start only, you would need:

    Code:
    if (!preg_match('~^ref_~i', $basename)  &&
    Last edited by MattF; 11-16-2010 at 03:43 AM.

  • Users who have thanked MattF for this post:

    bronzino (11-16-2010)

  • #3
    New Coder
    Join Date
    Mar 2010
    Location
    Chicago, Illinois
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Good grief, thank you - I spent waaaay too much time trying to sort that out on my own. I used that last solution and it worked precisely as desired.


  •  

    Posting Permissions

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