SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using preg_match with an array

    Hi

    I'm afraid I have a pretty basic knowledge of PHP.

    I've inherited a site with some php as follows:

    Code:
    if ($po = $product->GetLink) {$iext = preg_match ("/dwg/", $po) ? 'dwg' : 'pdf'; ?>
    ..............
    }


    I understand this to be
    1. $po is the file name
    2. $iext is the value 'dwg' if the string 'dwg' appears in $po
    3. else $iext is the value 'pdf'.


    This allows me use the value $iext in my html - assiging a css class to the item for styling.


    The thing is I would like to allow for more than either .dwg or .pdf files. (.doc, .zip or .bmp for example).

    So would I need to write something that does the following:

    1. Create an array of file types:


    Code:
    $filetypes = array (
        "filetypes"  => array("dwg" => ".dwg", "pdf" => ".pdf", "zip" => ".zip")
    );

    2. See if any values in the array match a string in my $po and declare $iext to be that match.

    Something like this..... except this doesn't work.

    Code:
    if (preg_match($filetypes, $po))
      {
       $iext = preg_match ($filetypes, $po); 
      }

    Am I way off course?

    Thanks

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure preg is what you're really looking for here.

    I assume that the file extension (the .dwg, .pdf, etc) will always appear at the end of the link.
    If my assumption is correct, then you're not really needing the power (and complexity) of preg, you just need a little simple string manipulation.
    PHP Code:
    $filetypes = array("zip","pdf","dwg");
    if (
    $po $product->GetLink) { //I dislike this line, but we'll roll with it.
      
    $ext end(explode(".",$po)); //Get the extension
      
    if (!in_array($ext,$filetypes)) { $ext ""; }  //Or FALSE, or whatever you want "It wasnt one of the known types" to be

    Why? Your preg_match is doing a location-insensitive search. So, if i name a file "dwg_mystuff.doc"... your preg_match will think it's a .dwg, because "dwg" exists in the string.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah.

    That's really good feedback and understood.
    But I now see my question led you in the wrong direction. Sorry.

    If I implemented your simpler suggestion a visitor would not know if the .zip contained a pdf file or a dwg file.

    So I can I see why the developer used location-insensitive preg_match to search for the file type anywhere in the name - and combined
    with a convention to specify the file type in a zip file name - so determine if its a dwg.... or not.

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Does the convention specify the location of the file type in the zip name? (IE: Is it always at the end? Always at the beginning? Seperated by a certain character?)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm afraid not.

    Until recently the preg_match searched for dwg at the end i.e.
    $iext = preg_match ("/dwg$/", $po)
    but I was asked to change this as zip files containing dwg files were being given a class of pdf.

    So I removed the $ to make it

    $iext = preg_match ("/dwg/", $po)
    and it appears to work satisfactorally.

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Ew.

    Well. I cant guarantee accuracy (because filenames could contain a set of characters naturally), but this would work...
    PHP Code:
    $matches = array(); //Because it's good practice to initialize your arrays, StarLion. *slaps wrist*
    preg_match("~dwg|pdf|zip~",$po,$matches)
    if(
    count($matches) > 0) {
    $iext $matches[0];
    } else {
    $iext "Not found"// or PDF, or... whatever you want "I didnt find it" to be. If everything is a .zip, you will always find the "zip" at the end as long as zip is in the pattern.

    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face

    Thank you for your help StarLion. I owe you.

    Just fyi I needed to tweak the code - adding an if clause to the beginning of the preg_match as otherwise the page error'd:

    PHP Code:
    $matches = array(); //Because it's good practice to initialize your arrays, StarLion. *slaps wrist*
    if (preg_match("~dwg|pdf|dgn|zip~"$po,$matches)) {
    if(
    count($matches) > 0) {
    $iext $matches[0];
    }

    else {
    $iext  'default';
    }

    Not sure this makes sense to you. But its working for me thank you very much.


Tags for this Thread

Bookmarks

Posting Permissions

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