简体   繁体   中英

how to get link from img tag

$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

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.

 function scrapeImage($text) {
    $pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/';
    preg_match($pattern, $text, $link);
    $link = $link[1];
    $link = urldecode($link);
    return $link;

}

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

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.

Try this code:

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

foreach($imageTags as $tag) {
    echo $tag->getAttribute('src');
}
?>
preg_match('/<img src=\("|')([^\"]+)\("|')[^\>]?>/', $img);

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);
?>

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;
}

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM