Solved

PHP: case insensitive glob

Posted on 2010-11-22
6
2,903 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 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
Technology Partners: 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
The viewer will learn how to dynamically set the form action using jQuery.
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…

752 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