Solved

Filtering files with regular expression

Posted on 2003-10-21
4
340 Views
Last Modified: 2006-11-17
I'm trying to filter files in a directory.
To do so, I need a function using regular expression that can accept 3 parameters:
a filename, a prefix and an extension and returns true (match) or false (no match)

My code at this moment is:

<?php
function CheckPattern($filename, $prefix, $ext) {
  $testExp = "^".$prefix."([\.\+\-\@\w\d])*\.".$ext."$";
  return ($testExp, $filename) );
}
$sPref = "ABC";
$sExt = "xml";
$file = "ABC-123asq.xml";
 if (CheckPattern($file, $sPref, $sExt)) {
     echo "Its a match";
 } else {
     echo "No match";
 }
?>

It should return a match in this example, but it doesn't :-(
I'm stucked on this.

Any help?
0
Comment
Question by:itamar
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
ThG earned 125 total points
ID: 9593952

function CheckPattern($filename, $prefix, $ext) {
 $testExp = "/^".$prefix."([\.\+\-\@\w\d])*\.".$ext."$/";
 return preg_match($testExp, $filename);
}

this is your fixed function *BUT* it's very bad coding, because regexp have many metacharacters, so inserting a raw parameter into the regular expression might cause unexpected behaviour.
Guess what would happen if you enforce suffix:
".jpg"
that would match "Ajpg", "5jpg", "?jpg"
0
 
LVL 4

Author Comment

by:itamar
ID: 9597985
Hi ThG,

I got your point.
But it is part of a bigger context.
Extensions are choosen from a list and prefix probably will be letters from A to Z (index)

Many thanks.

Itamar
0
 
LVL 4

Author Comment

by:itamar
ID: 9598016
Hi again,

looking at your code I could see the difference is in "/^" and "$/"

As I'm PHP beginner could you explain why it's necessary as a bonus ;-))

Thanks again
0
 
LVL 14

Expert Comment

by:ThG
ID: 9598282

OK :-)
That's not php related, that's the basic syntax for regular expressions. A regular expression is anclosed between two delimiters, no matter which they are, but you SHOULD use "/"
you were probably getting an error related to missing delimiter "^", because it thought you were using "^" and "^" as delimiters, which is totally wrong.
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

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

867 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

26 Experts available now in Live!

Get 1:1 Help Now