Jump to content

[SOLVED] Complete noob question - extract middle of filename


richrock

Recommended Posts

Right, today has been my first day of doing some coding using regex, and boy am I stumped :D

 

I've got a script to read a image folder, and need to extract parts of the filename in order to display information dynamically.  The images are set with a particular naming convention:

 

my real name_location-year-imagecode.jpg : I can get the name, year and image code, but for some reason I just don't know enough to get teh location.  It's got an underscore to the left and hyphen to the right, so how would I get it?

 

I was thinking along the lines of this

 

$regex = preg_match('/([^_.$/-])/', $filename);
		echo $regex;

Which obviously doesn't work, as this is rather hard, and I'm very tired :(  Id like it to have something like:

 

$regex = preg_match('/([_wildcardlocation-])/', $filename);

 

Thanks if you can help :D

beat me to it :D  Sometimes I pause when googling, ask here then find the answers been there all along  :-\

 

Thanks.  I did do it slightly differently, but will try this too, just to learn how this stuff works.

Given the format:

my real name_location-year-imagecode.jpg

 

(read - assuming the underscore is the only character within this which separates the real name from the location, and the dash is the only characters which separates the location from the year and the imagecode)... one could use preg_split:

 

$str = 'my real name_location-year-imagecode.jpg';
$arr = preg_split('#[_-]#', $str);
echo "<pre>".print_r($arr, true);

 

Output:

Array
(
    [0] => my real name
    [1] => location
    [2] => year
    [3] => imagecode.jpg
)

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.