Click to See Complete Forum and Search --> : Parsing?
SlicedCheese
Nov 16th, 2006, 11:00 AM
OK I'm trying to get the URL of an image on a page.
http://habbo.co.uk/home/Korma
The image im trying to get is in code like this in the src bit.
<div class="profile-figure">
<img alt="Korma" src="-----HERE-----" />
</div>
I've tried this code
<?php
$home = file_get_contents("http://www.habbohotel.co.uk/home/Korma");
$start = '<img alt="Korma" src="';
$end = '" />';
$pagedata = eregi("$start(.*)$end", $home, $output1);
echo $output1[1];
?>
But sometimes it works sometimes it doesnt work. Any help?
manavo11
Nov 16th, 2006, 02:22 PM
Does $home have the right value? I think it's simpler to just do it like this :
<?php
$home = file_get_contents("http://www.habbohotel.co.uk/home/Korma");
echo '<img alt="Korma" src="'.$home.'" />';
?>
But I have no comment for the first line (getting the $home value) as i don't know what it does or how it works :)
kows
Nov 16th, 2006, 04:18 PM
uhh, why would you set the source of an image to the HTML of a page?
you can probably find some way to do this only with a regular expression, but this works:
<?php
/*
find text:
---------------
<div class="profile-figure">
<img alt="Korma" src="/habbo-imaging/avatar/1750118001215012750529009014400143840e7c4cf9a45fc73cadb05847613.gif" />
</div>
---------------
*/
$domain = 'http://www.habbo.co.uk';
$content = file_get_contents($domain . '/home/Korma');
$div_start = explode('<div class="profile-figure">', $content, 2);
$div_end = explode('</div>', $div_start[1], 2);
$img = trim($div_end[0]); //this is <img alt="korma" sec="blah">
$img = preg_replace('/<img alt=\"(.*?)\" src=\"(.*?)\" \/>/', '$2', $img);
echo '<img src="' . ($domain . $img) . '" />';
?>
this will output:
<img src="http://www.habbo.co.uk/habbo-imaging/avatar/1750118001215012750529009014400143840e7c4cf9a45fc73cadb05847613.gif" />
visualAd
Nov 17th, 2006, 12:22 PM
OK I'm trying to get the URL of an image on a page.
http://habbo.co.uk/home/Korma
The image im trying to get is in code like this in the src bit.
<div class="profile-figure">
<img alt="Korma" src="-----HERE-----" />
</div>
I've tried this code
<?php
$home = file_get_contents("http://www.habbohotel.co.uk/home/Korma");
$start = '<img alt="Korma" src="';
$end = '" />';
$pagedata = eregi("$start(.*)$end", $home, $output1);
echo $output1[1];
?>
But sometimes it works sometimes it doesnt work. Any help?
If you use PHP 5, use DOM and XPath to get all the image:
$page = new DOMDocument(1.0);
$page->loadHTMLFile($url);
$xPath = new DOMXPath($page);
$img = $xPath->evaluate("//img[@alt='korma']")->item(0);
// now parse the url
parse_url($img->getAttribute('src'));
SlicedCheese
Nov 17th, 2006, 03:43 PM
Thanks everyone. I have it working.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.