dreamwest Posted September 19, 2009 Share Posted September 19, 2009 Ive almost got this but i cant seem to get it to stop at the first / $link = "site.com/bfdgnh/oc8687/funny_names.jpg"; if (preg_match_all("/\/(.*)\.jpg/i", $link, $titles)) { print_r($titles); } This outputs: Array ( [0] => Array ( [0] => /bfdgnh/oc8687/funny_names.mp3 ) [1] => Array ( [0] => bfdgnh/oc8687/funny_names ) ) but i just need the last part "funny_names" Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 19, 2009 Share Posted September 19, 2009 This really belongs in the regex section. Try "~/(.*?)\.jpg~s" as your pattern Quote Link to comment Share on other sites More sharing options...
knsito Posted September 19, 2009 Share Posted September 19, 2009 Start your string with "http://" and use parse_url() and pathinfo() $link = "http://site.com/bfdgnh/oc8687/funny_names.jpg"; $p = parse_url($link); var_dump($p); $p = pathinfo($p['path']); var_dump($p); echo $p['filename']; Quote Link to comment Share on other sites More sharing options...
MadTechie Posted September 19, 2009 Share Posted September 19, 2009 I would probably do this, if (preg_match('%([^/.]+)\.?[^/.]{0,3}$%', $link, $regs)) { $titles= $regs[1]; } EDIT: typo Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 20, 2009 Share Posted September 20, 2009 basename() is made for this: $link = 'site.com/bfdgnh/oc8687/funny_names.jpg'; echo basename($link, '.jpg'); Quote Link to comment Share on other sites More sharing options...
MadTechie Posted September 20, 2009 Share Posted September 20, 2009 basename is the best option if it's always .jpg, but I assumed the extension would be anything. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 20, 2009 Share Posted September 20, 2009 basename is the best option if it's always .jpg, but I assumed the extension would be anything. Could alternatively be taken care of by using substr() and strrpos(): $link = 'site.com/bfdgnh/oc8687/funny_names.jpg'; $filename = basename($link); $lastdot = strrpos($filename, '.'); if ($lastdot !== false) {$filename = substr($filename, 0, $lastdot);} echo $filename; Quote Link to comment Share on other sites More sharing options...
MadTechie Posted September 20, 2009 Share Posted September 20, 2009 You could but that's would probably be slower than the RegEx. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 20, 2009 Share Posted September 20, 2009 Probably. But then again, your pattern fails when the filename contains a dot Quote Link to comment Share on other sites More sharing options...
MadTechie Posted September 20, 2009 Share Posted September 20, 2009 that's easy to fix, just drop the greediness $link = "site.com/bfdgnh/oc8687/funny_n.ames.jpg"; if (preg_match('%([^/]*?).?[^/.]{0,3}$%', $link, $regs)) { $titles= $regs[1]; } echo $titles; Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 21, 2009 Share Posted September 21, 2009 Yeah, I know, I'm just mocking you Fun fact: My method using the built in string functions is ~ 38 times faster than your method using regex (0.052 seconds versus 1.965 seconds when run 10000 times in a loop). Edit: And actually, your pattern cuts the last three chars off of the filename if it hasn't got any extension. Might not be relevant in the OP's case, but could be. But in the big picture, I prefer to write and use code that is easily readable, even at the possible (slight) cost of efficiency. And when the simpler method in this case is faster than the more complicated, harder to read regex, I find it an easy choice. But each man has his own preferences. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted September 21, 2009 Share Posted September 21, 2009 I ran some tests here and yes its quicker but i had different results (i fixed the no dot problem) only real issue with the basefile is if you have a dot but no extention ie hello.world would be dropped the world, so the RegEx has a little more control but slightly slower.. i hashed the names to stop the caching. <?php //0.255233 $start = microtime(); for($i=1;$i<5000;$i++) { $h = md5($i); $link = "site.com/bfdgnh/$h/$h.jpg"; $filename = basename($link); $lastdot = strrpos($filename, '.'); if ($lastdot !== false) {$filename = substr($filename, 0, $lastdot);} } echo microtime() - $start."\n"; ?> <?php //0.306391 $start = microtime(); for($i=1;$i<5000;$i++) { $h = md5($i); $link = "site.com/bfdgnh/$h/$h.jpg"; preg_match('/(.*?)(?:\.[^.]{0,3})?$/', $link,$regs); $titles= $regs[1]; } echo microtime() - $start."\n"; ?> as we have taken over this thread.. wanna run the scripts at your side see if you get the same.. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 21, 2009 Share Posted September 21, 2009 only real issue with the basefile is if you have a dot but no extention ie hello.world would be dropped the world Technically there is no such thing. world would be the extension in that example. Extensions can be longer than 3 chars, so it doesn't make sense to keep them based on length. And good idea with the hashing. When I run your code the string function method is still over 4 times faster than the regex method (0.045751 seconds versus 0.200152 seconds). I'm testing with PHP 5.2.6 @ Apache 2.2.9 on a quad-core Vista box. 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.