Search code examples
phppreg-replacepreg-match

PHP get file name from URL stripping extension and image dimesion


Following is one of the dynamically generated variable i have http://www.niresh.guru/wp-content/uploads/2016/06/We-are-all-visual-creators-469x1024.jpg

I want to get final output of filename stripping extension jpg and image dimesion 469x1024 and final name in lowercase this is the final output im expecting we-are-all-visual-creators Note hyphen before the image dimension also needed to be removed

I need a common function to strip image dimension and image extensions like jpg, png, jpeg and tiff (case insensitive)

Im using my iPhone to ask this question i have tried a few php by researching in internet, due to powercut I can't use my mac these are all the info i can provide thanks.

please do not downrate if you have any questions comment


Solution

  • Is that what you want:

    $url = " http://www.niresh.guru/wp-content/uploads/2016/06/We-are-all-visual-creators-469x1024.jpg";
    preg_match('~^.+/([\w-]+)-(\d+x\d+)(\.\w+)$~', $url, $match);
    print_r($match);
    

    Output:

    Array
    (
        [0] =>  http://www.niresh.guru/wp-content/uploads/2016/06/We-are-all-visual-creators-469x1024.jpg
        [1] => We-are-all-visual-creators
        [2] => 469x1024
        [3] => .jpg
    )