Solved

PHP: case insensitive glob

Posted on 2010-11-22
6
2,772 Views
Last Modified: 2012-05-10
Using glob(), I want to find ALL files ending in .html, regardless of their case.

My code is case sensitive.

I want to find ALL these files:

      1.html
      2.HTML
      3.htML

foreach ( glob ( "*.hTmL") as $f) {
var_dump($f);
}

Open in new window

0
Comment
Question by:hankknight
6 Comments
 
LVL 10

Assisted Solution

by:Chad Smith
Chad Smith earned 100 total points
ID: 34191581
glob('*.[hH][tT][mM][lL]')
0
 
LVL 16

Author Comment

by:hankknight
ID: 34192221
Yes, but can this be done more dynamically?  The extension type comes from a user-provided variable so this needs to support more than .html extensions.

0
 
LVL 10

Expert Comment

by:Chad Smith
ID: 34192249
$files = glob('my/dir/*');
$myFiles =  preg_grep('/\.html$/i', $files);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Assisted Solution

by:tel2
tel2 earned 50 total points
ID: 34192617
Or in case you haven't worked it out hankknight,, expanding on knight00's solution, you could add valid extensions like this:

    $files = glob('my/dir/*');
    $myFiles =  preg_grep('/\.html$|\.htm$|\.php|...etc.../i', $files);

BTW, sorry if this was meant to be a Knight family only, gathering.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 300 total points
ID: 34192971
Another alternative:
$ext = "hTmL";

$ext = "*." . preg_replace('/(\w)/e', "'[' . strtoupper($1) . strtolower($1) . ']'", $ext);

foreach (glob($ext) as $f)
{
	var_dump($f);
}

Open in new window

0
 
LVL 13

Assisted Solution

by:marchent
marchent earned 50 total points
ID: 34209339
There is a deprecated function sql_regcase(). You can use this function. Check more from http://bd.php.net/sql_regcase


$pattern = sql_regcase("*.hTmL");

var_dump($pattern);



foreach ( glob( $pattern ) as $f) {

    var_dump($f);

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Animated .jpg? 13 59
How to use md5 hashing 3 26
Change text to radio button and calendar form 2 32
How to keep line breaks when outputting data from database 4 14
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now