Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    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

    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

  • #2
    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
    Found it, missed the leading "."

    Should be

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

    Thanks for letting me talk to myself.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    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
    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
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •