Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP: case insensitive glob

Posted on 2010-11-22
6
Medium Priority
?
3,016 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 10

Assisted Solution

by:Chad Smith
Chad Smith earned 400 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
Industry Leaders: 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 200 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 1200 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 dynamically set the form action using jQuery.

719 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