Solved

Filtering files with regular expression

Posted on 2003-10-21
4
347 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

860 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