View Full Version : Regular Expressions

02-05-2003, 11:01 PM
I'm trying to use the preg_match function but can't get it to work right.

The variable $nFile will contain a string which is a path to a file pulled from a DB. (ex: ./docs/myfile.doc)

Since the file can either be a .doc or a .pdf, I want to check if its a .doc and make the header for the document application/msword and make it application/pdf otherwise.

My code is below.

if(preg_match("/*.doc/",$nFile)) {
print "DOC";
else {
print "NOT DOC";

it gives me this error message:
Warning: Compilation failed: nothing to repeat at offset 0 in c:\dreamweaver\chapters\shownewsletter.php on line 15

I've also tried

if(preg_match("/*.doc/",$nFile,$matches)) {
print "DOC";
else {
print "NOT DOC";

Any help, along with a better explaination than the documentation provided from php would be a great help.


02-05-2003, 11:12 PM
Found it, missed the leading "."

Should be


Thanks for letting me talk to myself.

02-06-2003, 11:57 AM
on an aslant:

if you are only using .doc and .pdf, why not lose the slightly heavy regexing and just pull the last three chars?

$f_type = substr($nFile,-3);
if($f_type == 'doc') header("ummm doc");
else(..... == 'pdf') header.....

02-06-2003, 01:55 PM
In the future I will be allowing additional extensions, .html, .htm, .txt most of which will be 3 letters but some will be more. So my regex will actually be something like

if(preg_match($re, $nFile) {
//do something

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum