Jump to content

[SOLVED] How to get image name using preg_match_all


t_machine

Recommended Posts

hi, i am using a tutorial I found that gets the image src, image type but does not show how to get the name itself.

 

example:

$data = 'Some text here <img src="images/myimg.jpg" border="0"> more text.';
   $pattern = "/src=[\"']?([^\"']?.*(png|jpg|gif))[\"']?/i";
   preg_match_all($pattern, $data, $images);
  echo $images[0][0]; //src=images/myimg.jpg
  echo $images[1][0]; //images/myimg.jpg
  echo $images[2][0]; //jpg 

 

How can I get "myimg" from the src url?

 

Thanks for any help :)

<pre>
<?php
$data = 'Some text here <img src="images/myimg.jpg" border="0"> more text.';
preg_match_all('/src=([\'"])?((?(1).*?|\S+))(?(1)\1)/', $data, $images);
foreach ($images[2] as $src) {
	print_r(pathinfo($src));
}
?>
</pre>

Archived

This topic is now archived and is closed to further replies.

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