如何使用PHP或正则表达式提取图像名称?

I have the following data in MySQL.

<p><img src="../../../../assets/images/frontpage/image1.png" 
alt="" width="790" height="356" /></p>

Now I want to get image2.png with PHP or regex. The extension can be gif or jpg. And a length of image name can be any length.

This would match a path in an img tag and capture the file in the first interior capturing group.

<?php
if (preg_match('%<img\s.*?src=".*?/?([^/]+?(\.gif|\.png|\.jpg))"%s', $subject, $regs)) {
    $image = $regs[1];
} else {
    $image = "";
}
?>

This code should do what you need:

<?php
$regex = '@src[ ]*=[ ]*"[a-z/.]*/(.*?\.(?:png|gif|jpg))@i"';
$match = array();
if (preg_match($regex, $html, $match)) {
    $imglocation = $match[1];
} else {
    die('Failed to find image name.');
}

Use this expression: var expr=/[a-z]+\.(gif|jpg)$/;

Try this:

$str = '<p><img src="../../../../assets/images/frontpage/image1.png" 
alt="" width="790" height="356" /></p>'; 
$imageType = end(explode("."));