|
-
Nov 16th, 2006, 12:00 PM
#1
Thread Starter
Lively Member
Parsing?
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.
HTML Code:
<div class="profile-figure">
<img alt="Korma" src="-----HERE-----" />
</div>
I've tried this code
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?
-
Nov 16th, 2006, 03:22 PM
#2
-
Nov 16th, 2006, 05:18 PM
#3
Re: Parsing?
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 Code:
<?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:
Code:
<img src="http://www.habbo.co.uk/habbo-imaging/avatar/1750118001215012750529009014400143840e7c4cf9a45fc73cadb05847613.gif" />
-
Nov 17th, 2006, 01:22 PM
#4
Re: Parsing?
 Originally Posted by SlicedCheese
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.
HTML Code:
<div class="profile-figure">
<img alt="Korma" src="-----HERE-----" />
</div>
I've tried this code
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:
PHP Code:
$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'));
-
Nov 17th, 2006, 04:43 PM
#5
Thread Starter
Lively Member
Re: Parsing?
Thanks everyone. I have it working.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|