Help required pulling data from web page using "preg_match_all();"

Discussion in 'PHP' started by centralexpert, Sep 18, 2011.

  1. #1
    Hi,

    I'm trying to pull some data from a web page using preg_match_all();.

    Following is a sample of the code output i'm pulling from. 2 rows of the table the data is stored in.

    There is approx 350 rows of data on the page and the info i need to get is for example the player id "<td>808</td>" and player points which are relevant to that player which is in this case is "<td>21</td>".

    
    <tr class="odd">
        <td>STR</td>
        <td>808</td> <!--// Player Id //-->
        <td class="leftAlign"><a href="/ViewPlayerProfile.aspx?pid=808">R van Persie</a></td>
        <td class="leftAlign">Arsenal</td>
        <td>2.8</td>
        <td>£7.5m</td>
        <td>21</td> <!--// Points //-->
    </tr>					
    <tr class="even">
        <td>STR</td>
        <td>807</td> <!--// Player Id //-->
        <td class="leftAlign"><a href="/ViewPlayerProfile.aspx?pid=807">M Chamakh</a></td>
        <td class="leftAlign">Arsenal</td>
        <td>0.0</td>
        <td>£4.5m</td>
        <td>0</td> <!--// Points //-->
    </tr>
    
    HTML:
    Would someone be able to point me in the right direction?

    I used to use preg_match_all(); last year but since they have changed the layout slightly the code no longer works...

    Regards

    Central
     
    centralexpert, Sep 18, 2011 IP
  2. Zinosi

    Zinosi Well-Known Member

    Messages:
    75
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    118
    #2
    What is the existing code that you are using?
     
    Zinosi, Sep 18, 2011 IP
  3. centralexpert

    centralexpert Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi the original code was written as a function which was as follows:

    
    function UpdatePlayerStats()
    	{
    	DBConnect();
    	$url = file_get_contents("URL_GOES_HERE");
    	preg_match_all('~<td class="vfm centerAlign lastCol">(.*?[^<])</td>~i',$url,$points);
    	for($i=0;$i<count($points[0]);$i++)
    		{
    		$point = preg_replace('/<td class="vfm centerAlign lastCol">/','',$points[0][$i]);
    		$sql = "UPDATE Players SET points = '".trim(preg_replace('/<\/td>/','',$point))."' WHERE id = '$i'";
    		$result = mysql_query($sql) or die(mysql_error());
    		}
    	mysql_close();
    	}
    
    PHP:
    Problem is that the <td class="vfm centerAlign lastCol">points</td> no longer exists and has been replaced with just <td>points</td>.

    How can I decipher which <td></td> to grab?

    Regards
     
    centralexpert, Sep 19, 2011 IP