Php在链接中获取图像名称

i am using a script, to get the first image, inside a post.

Here is the script.

$first_img = '';
                $my1content = $row['post_content'];
                $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $my1content, $matches); 
                $first_img = $matches [1] [0];
                if(empty($first_img)){ //Defines a default image
                $first_img = "/img/default.png";
                }

This script echo the full image link, for example: http://mywebsite.com/images/thisistheimage.jpg

is possible to divide the image link , image name , and image extenction so i need to get 3 results

the link, example:http://mywebsite.com/images/ the image name, example: thisistheimage the image extenction, example: .jpg

Please let me know if its all clear, thanks for reading.

<?php
  $image_name       = pathinfo( $first_img, PATHINFO_FILENAME );
  $image_extension  = pathinfo( $first_img, PATHINFO_EXTENSION );
  $image_with_extension = basename( $first_img );
  $image_directory      = dirname( $first_img );
?>

Check out the built-in PHP function pathinfo. Looks like just what you need.

You can use the built-in function pathinfo() to parse the src for what you want.

$path_parts = pathinfo('/img/default.png');

echo $path_parts['dirname'], "
";         // /img
echo $path_parts['basename'], "
";        // default.png
echo $path_parts['extension'], "
";       // .png
echo $path_parts['filename'], "
";        //  default

The PHP reference is here