Jump to content

Preg_Match


Lytheum

Recommended Posts

I need helping using this function to find some numbers out of a string of html. The html is the following:

[code]
<div class="mainscroll-content"><table cellpadding="3" cellspacing="0" border=0>
<tr>

<td align="left"><a href="overall.ws?table=0&user=zezima">Overall</a></td>
<td align="right">2</td>
<td align="right">2164</td>
<td align="right">554,475,897</td>

</tr></table></div>[/code]

Now, I need to find out what is between the first <td></td>, 2nd, 3rd, and fourth - all dynamically (I don't know the numbers yet, this is an example.

This is how I have started it:

[code]preg_match_all ("/<div class=\"mainscroll-content\">([^`]*?)<\/div>/", $data, $matches);

// Loop through each content item
foreach ($matches[0] as $match) {
    // Stat
    preg_match ("/<a href=\"overall.ws?table=0&user= . $user\">([^`]*?)<\/a><\/td>", $match, $temp);
    $stat = $temp['1'];
    $stat = strip_tags($stat);
    $stat = trim($stat);
    // Rank
    preg_match ("/<td align=\"right\">([^`]*?)<\/td>/", $match, $temp);
    $rank = $temp['1'];
    $rank = trim($rank);
   // Level
    preg_match ("/<td align=\"right\">([^`]*?)<\/td>/", $match, $temp);
    $level= $temp['1'];
    $level = trim($level);
   // Exp
    preg_match ("/<td align=\"right\">([^`]*?)<\/td>/", $match, $temp);
    $exp = $temp['1'];
    $exp = trim($exp);[/code]

It works..almost. It finds what is between the first <td></td>. for each different one I called. So it all finds the same data (duh).

Sorry for the long code, and if I explained it wrong I will retry tomorrow (1am).
Link to comment
Share on other sites

Changed your regular expression, and now using preg_match_all:

[code]<?php

$str = '<td align="left"><a href="overall.ws?table=0&user=zezima">Overall</a></td>
<td align="right">2</td>
<td align="right">2164</td>
<td align="right">554,475,897</td>';

$pattern = '/<td(?:[^>])*>(.*?)<\/td>/s';
preg_match_all($pattern, $str, $m);

$temp = array_map('trim', $m[1]);

echo '<pre>';
print_r($temp);
echo '</pre>';

?>[/code]

Which outputs

[code]Array
(
    [0] => <a href="overall.ws?table=0&user=zezima">Overall</a>
    [1] => 2
    [2] => 2164
    [3] => 554,475,897
)[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.