bundyxc Posted July 17, 2009 Share Posted July 17, 2009 haha, with that in mind, I have a question. I want to extract a user's MySpace picture, using their Friend ID. A friend ID comes up at the end of a URL. http://profile.myspace.com/index.cfm?fuseaction=user.viewProfile&friendID=6221 This has the friendID of "6221" (as I'm sure you can see). Now, if you follow that link, it will take you to a profile. I want to grab the image that he's using as his default picture. The image is in this TD <td class="text" width="75" bgcolor="#ffffff" height="75"> <a id="ctl00_cpMain_ctl01_UserBasicInformation1_hlDefaultImage" rel="searchMonkey-photo" href="http://viewmorepics.myspace.com/index.cfm?fuseaction=user.viewAlbums&friendID=6221"> <img border="0" alt="" src="http://b2.ac-images.myspacecdn.com/00000/20/52/2502_m.jpg" /> </a> <td> <img border="0" alt="" src="http://b2.ac-images.myspacecdn.com/00000/20/52/2502_m.jpg" /> How would I extract this image, if I only had the friendID? I need a script that will go to the page, and grab the image. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/166286-simple-extracting-some-html-from-a-page/ Share on other sites More sharing options...
.josh Posted July 17, 2009 Share Posted July 17, 2009 '~<a[^>]*friendID='.$friendID.'[^>]*>(.*?)</a>~is' Quote Link to comment https://forums.phpfreaks.com/topic/166286-simple-extracting-some-html-from-a-page/#findComment-877025 Share on other sites More sharing options...
bundyxc Posted July 18, 2009 Author Share Posted July 18, 2009 Thank you! Could you advise me on how to find the URL of the image, instead of the img tag? Text should be found after: <img border="0" alt="" src="http://b2.ac-images.myspacecdn.com/00000/20/52/2502_m.jpg But before: " /> Quote Link to comment https://forums.phpfreaks.com/topic/166286-simple-extracting-some-html-from-a-page/#findComment-877502 Share on other sites More sharing options...
ghostdog74 Posted July 18, 2009 Share Posted July 18, 2009 just use normal string functions $url = "http://profile.myspace.com/index.cfm?fuseaction=user.viewProfile&friendID=6221"; $page = strip_tags(file_get_contents($url),"<img>"); $p = explode("/>",$page); foreach($p as $k=>$v){ if(strpos($v,"img class=")!==FALSE) { $jpg = explode("src=",$v); $imagelink = preg_replace("/\"/","",$jpg[1]); #remove doublequotes print "imaglink " . $imagelink; $image = file_get_contents($imagelink); $f=fopen("friend.jpg","w"); fwrite($f,$image); fclose($f); } } Quote Link to comment https://forums.phpfreaks.com/topic/166286-simple-extracting-some-html-from-a-page/#findComment-877544 Share on other sites More sharing options...
bundyxc Posted July 18, 2009 Author Share Posted July 18, 2009 wow, thank you. Now I feel a bit stupid. I've been spending the last two hours or so studying regex, and haven't gotten anywhere, but now I guess it was something completely different. String functions, you said? Thanks a ton. Does this work if the image has a PNG or other image types? Quote Link to comment https://forums.phpfreaks.com/topic/166286-simple-extracting-some-html-from-a-page/#findComment-877637 Share on other sites More sharing options...
nrg_alpha Posted July 18, 2009 Share Posted July 18, 2009 Alternatively (using DOM / XPath): $url = "http://profile.myspace.com/index.cfm?fuseaction=user.viewProfile&friendID=6221"; $dom = new DOMDocument; @$dom->loadHTMLFile($url); $xpath = new DOMXPath($dom); $imgTag = $xpath->query('//a[contains(@href, "friendID' . strrchr($url, '=') . '")]/img'); foreach ($imgTag as $val) { echo $val->getAttribute('src') . "<br />\n"; // Output: http://b2.ac-images.myspacecdn.com/00000/20/52/2502_m.jpg } Quote Link to comment https://forums.phpfreaks.com/topic/166286-simple-extracting-some-html-from-a-page/#findComment-877656 Share on other sites More sharing options...
nrg_alpha Posted July 19, 2009 Share Posted July 19, 2009 Instead of fetching the info using foreach (since there is only 1 result anyway), you could alternatively tap into $imgTag's item(0) [DOMElement] like so: echo $imgTag->item(0)->getAttribute('src'); Quote Link to comment https://forums.phpfreaks.com/topic/166286-simple-extracting-some-html-from-a-page/#findComment-878146 Share on other sites More sharing options...
ghostdog74 Posted July 20, 2009 Share Posted July 20, 2009 Thanks a ton. Does this work if the image has a PNG or other image types? i have not specifically harded any image file type. as you can see, my code searches for "img class" , therefore, if the link is to a png file, it should still grab for you Quote Link to comment https://forums.phpfreaks.com/topic/166286-simple-extracting-some-html-from-a-page/#findComment-878366 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.