Results 1 to 5 of 5

Thread: Parsing?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    83

    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?

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Parsing?

    Does $home have the right value? I think it's simpler to just do it like this :

    PHP Code:
    <?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


    Has someone helped you? Then you can Rate their helpful post.

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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">'$content2);
      
    $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" />

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Parsing?

    Quote 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')); 

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    83

    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
  •  



Click Here to Expand Forum to Full Width