Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-05-2003, 10:01 PM   PM User | #1
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
Regular Expressions

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.

Code:
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
Code:
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.

Thanks
bcarl314 is offline   Reply With Quote
Old 02-05-2003, 10:12 PM   PM User | #2
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
Found it, missed the leading "."

Should be

preg_match("/.*.doc^/i",$nFile)

Thanks for letting me talk to myself.
bcarl314 is offline   Reply With Quote
Old 02-06-2003, 10:57 AM   PM User | #3
Ökii
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 577
Thanks: 0
Thanked 0 Times in 0 Posts
Ökii is an unknown quantity at this point
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.....
__________________
Ökii - formerly pootergeist
teckis - take your time and it'll save you time.
Ökii is offline   Reply With Quote
Old 02-06-2003, 12:55 PM   PM User | #4
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
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

$ext=".html";
$re="/.*.".$ext."^/";
if(preg_match($re, $nFile) {
//do something
}
bcarl314 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:53 PM.


Advertisement
Log in to turn off these ads.