0
$img = '<img src="http://some-img-link" alt="some-img-alt"/>';

$src = preg_match('/<img src=\"(.*?)\">/', $img);

echo $src;

I want to get the src value from the img tag and maybe the alt value

Whisperity
  • 3,012
  • 1
  • 19
  • 36
Marian Petrov
  • 625
  • 2
  • 9
  • 21
  • 2
    "maybe the alt value" you either do or don't, coding "maybe" is very hard –  Jul 10 '12 at 04:10
  • if you learn to accept the answer that answered your question, you will find that you get much more consistent answers. this is also the way we show thanks here on stackoverflow – Max Hudson Jul 10 '12 at 13:21

9 Answers9

2

Assuming you are always getting the img html as you shown in the question.

Now in the regular expression you provided its saying that, after the src attribute its given the closing tag for img. But in the string there is an alt attribute also. So you need to care about it also.

/<img src=\"(.*?)\".*\/>/

And if you are going to check alt also then the regular expression.

/<img src=\"(.*?)\"\s*alt=\"(.*?)\"\/>/

Also you are just checking whether its matched or not. If you need to get the matches, you need to provide a third parameter to preg_match which will fill with the matches.

$img = '<img src="http://some-img-link" alt="some-img-alt"/>';
$src = preg_match('/<img src=\"(.*?)\"\s*alt=\"(.*?)\"\/>/', $img, $results);
var_dump($results);

Note : The regex given above is not so generic one, if you could provide the img strings which will occur, will provide more strong regex.

Jithin
  • 2,594
  • 1
  • 22
  • 42
  • example of img str : `$img = 'avatar from gravatar';` – Marian Petrov Jul 10 '12 at 05:05
  • thats becuase in the img string provided in the question is ended with a '/>' and the example above is with '>'. Better will remove that part from regex so that it won't break even if there is more attributes. So regex will be like / – Jithin Jul 10 '12 at 05:10
1
 function scrapeImage($text) {
    $pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/';
    preg_match($pattern, $text, $link);
    $link = $link[1];
    $link = urldecode($link);
    return $link;

}
Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
1

Tested Code :

$ input=’<img src= ”http://www.site.com/file.png” > ‘;
preg_match(“<img.*?src=[\"\"'](?<url>.*?)[\"\"'].*?>”,$input,$output);
echo $output;   // output = http://www.site.com/file/png
Hardik Sondagar
  • 4,347
  • 3
  • 28
  • 48
0

How to extract img src, title and alt from html using php?

See the first answer on this post.

You are going to use preg_match, just in a slightly different way.

Community
  • 1
  • 1
Max Hudson
  • 9,961
  • 14
  • 57
  • 107
0

Try this code:

<?php
$doc = new DOMDocument();
$doc->loadHTML('<img src="" />');
$imageTags = $doc->getElementsByTagName('img');

foreach($imageTags as $tag) {
    echo $tag->getAttribute('src');
}
?>
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Vikram Jain
  • 5,498
  • 1
  • 19
  • 31
0
preg_match('/<img src=\("|')([^\"]+)\("|')[^\>]?>/', $img);
j0k
  • 22,600
  • 28
  • 79
  • 90
Allen Chak
  • 1,802
  • 1
  • 10
  • 21
  • This may or may not work for all cases, since `'` may be used for quoting string; in some case, the quote might not be there at all (bad HTML). – nhahtdh Jul 10 '12 at 04:24
  • @ColeJohnson: Rejected. You forgot to escape `'`. And I think it is better to write your own answer. – nhahtdh Jul 10 '12 at 04:43
0

Also you could use this library: SimpleHtmlDom

<?php
$html = new simple_html_dom();
$html->load('<html><body><img src="image/profile.jpg" alt="profile image" /></body></html>');
$imgs = $html->find('img');
foreach($imgs as $img)
print($img->src);
?>
Allen Chak
  • 1,802
  • 1
  • 10
  • 21
0

You already have good enough responses above, but here is another one code (more universal):

function retrieve_img_src($img) {
  if (preg_match('/<img(\s+?)([^>]*?)src=(\"|\')([^>\\3]*?)\\3([^>]*?)>/is', $img, $m) && isset($m[4]))
    return $m[4];
  return false;
}
-2

You can use JQuery to get src and alt attributes

include jquery in header

 <script type="text/javascript" 
           src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
 </script>

//HTML

//to get src and alt attributes

<script type='text/javascript'>
 // src attribute of first image with id =imgId

 var src1= $('#imgId1').attr('src');
 var alt1= $('#imgId1').attr('alt');

 var src2= $('#imgId2').attr('src');
 var alt2= $('#imgId2').attr('alt');
</script>
Naveen Kumar
  • 4,543
  • 1
  • 18
  • 36