phphtmlregexhtml-parsingpreg-match

Extract text from div elements with specified class


I will have a table an and each table there might be 1 - 6 references to votes what I need is a regex to get the vote amount, My table looks something like:

<tbody><tr>
 </tr>
 <tr>
 <td>
 <div class="tp">
 10 VOTES
 </div>
 </td>
 </tr>
 <tr>
 <td>
 <div class="tp">
 9 VOTES
 </div>
 </td>
 </tr>


Solution

  • As I said in my comment, you shouldn't try to use a regex to parse HTML, as HTML is not a regular language.

    That said, this is a simple enough case, so if you were to use a regex here it might look something like this:

    $table = '<your html here>';
    
    if(preg_match_all('|^\s*(\d+)\sVOTES|m', $table, $matches)) {
        foreach($matches as $match) {
            echo "votes: $match";
        }
    }
    

    Note that this is highly dependent on the structure of your HTML, and again I really, really do not recommend using a regex for this (use an XML parser!). But you certainly could, as long as you're certain your HTML string will always be consistently formatted.