Search code examples
javascriptphphtmlhyperlinkinnertext

get contents of a link with php


How can I use PHP (javascript is fine too) to get the data from this?

    <div class="row first-row">
<a href="http://services.runescape.com/m=clan-hiscores/ranking?clanId=346923&amp;ranking=num_members">
<div class="left">
<p>
Clanmates
</p>
<span>
165
</span>
</div>
</a>
<div class="middle">
<img src="http://www.runescape.com/img/clan/stats/stats/num_members.png" alt="" title="Clanmates">
</div>
</div>

(note i ruined it because it parsed)

I want to return that '165' automatically, because it changes.

Sorry for asking a stupid question, searching google and this site doesn't come up with what I need.

I am new at this :/


Solution

  • You need to use DOMDocument to parse the HTML in question and extract the link text. Without more code or more information on what you are trying to do, it is hard to give a full answer.

    EDIT: Here is a working example with DOMDocument:

    <?php
    
    // Replace this with your actual HTML
    $str = '<td class="col3 align"><a href="http://somesite.com/">165</a></td>';
    
    // Create an instance of DOMDocument
    $html = new DOMDocument();
    // load and parse your HTML
    $html->loadHTML($str);
    // Get the a tags
    $a_tags = $html->getElementsByTagName('a');
    // Loop through them
    foreach ($a_tags as $link) {
        echo $link->nodeValue, PHP_EOL; // prints 165
    }
    

    IF the code really is broken HTML like in the sample you gave, you might be able to use a regular expression, like this:

    $str = 'td class="col3 align">a href="http://somesite.com/">165</a</td ';
    $text = preg_replace('~^.+?a[^>]+>|</a.+$~', '', $str);