Solved

PHP: case insensitive glob

Posted on 2010-11-22
6
2,873 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

740 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