richrock Posted April 20, 2009 Share Posted April 20, 2009 Right, today has been my first day of doing some coding using regex, and boy am I stumped 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 Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 20, 2009 Share Posted April 20, 2009 $location = reset(explode('-', end(explode('_', $filename)))); That might work Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 20, 2009 Share Posted April 20, 2009 Yeah, it does, I just checked it. Enjoy. Quote Link to comment Share on other sites More sharing options...
richrock Posted April 20, 2009 Author Share Posted April 20, 2009 beat me to it 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. Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 20, 2009 Share Posted April 20, 2009 Could you post your method? Just so people looking at this thread in the future will know how to do it. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 20, 2009 Share Posted April 20, 2009 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 ) Quote Link to comment Share on other sites More sharing options...
richrock Posted May 1, 2009 Author Share Posted May 1, 2009 even better - many thanks! Really should learn more regex Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.