Saturday, 3 November 2012

How to extract image name from the image url in php?

Sometimes we need to extract image name from the url of the image. For that use the below preg_match script:

<?php
if (preg_match('".*?/?([^/]+?(\.gif|\.png|\.jpg))"', $url, $regs)) {
    echo $image = $regs[1];
} else {
    $image = "";
}

?>
$url is the url of the image and $regs[1] gives the name of the image.Thats it!

No comments:

Post a Comment