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

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.