Solved

PHP: case insensitive glob

Posted on 2010-11-22
6
2,724 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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

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 74

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

705 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

15 Experts available now in Live!

Get 1:1 Help Now