Using preg_match with an array


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

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

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:
$filetypes = array (
    "filetypes"  => array("dwg" => ".dwg", "pdf" => ".pdf", "zip" => ".zip")
  1. 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.

if (preg_match($filetypes, $po))
   $iext = preg_match ($filetypes, $po);

Am I way off course?


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.

$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.


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.

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?)

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.


Well. I cant guarantee accuracy (because filenames could contain a set of characters naturally), but this would work…

$matches = array(); //Because it's good practice to initialize your arrays, StarLion. *slaps wrist*
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.

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:

$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.