Jump to content

preg_match_all


dreamwest

Recommended Posts

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"

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.