Jump to content

Need some help with preg_match_all


savj14

Recommended Posts

I am parsing out some data and I want to strip out (#numbers) from the output examples below.

I am using preg_match_all to do the parsing but can't seem to strip this.

 

Highest Skill - 29 (#79852)

Total EXP - 299  (#62976)

 

Here is what I am using to get the strings from above:

preg_match_all("|<tr(.*)</tr>|U",$table,$rows);

foreach ($rows[0] as $row){

    if ((strpos($row,'<th')===false)){
   
        preg_match_all("|<td(.*)</td>|U",$row,$cells);
       
        $title = strip_tags($cells[0][0]);
       
        $rank = strip_tags($cells[0][1]);
       
        $position = strip_tags($cells[0][2]);
          
   
	 echo "{$title} - {$rank}<br>\n";
   
    }

}

 

I assume i need to modify preg_match_all("|<tr(.*)</tr>|U",$table,$rows); to exclude (#numbers). But I don't know how to do this.

 

Can some one please help?

Link to comment
Share on other sites

That works except I need to strip this when parsing.

 

So I guess my question is how do I modify preg_match_all("|<tr(.*)</tr>|U",$table,$rows); with the code you provided? Basically I need to exclude (#numbers) somehow in preg_match_all

Link to comment
Share on other sites

So if I try to use what you suggested on its own it works....When I try it in my code it doesn't work

 

<?php

$url = "http://www.halo3leaderboard.com/ManHands14";

$raw = file_get_contents($url);

$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");

$content = str_replace($newlines, "", html_entity_decode($raw));

//$start = strpos($content,'<table cellpadding="2" class="standard_table"');
$start = strpos($content,'<table class="DataDisplay" width="100%" border="0" cellspacing="0" cellpadding="0">');

//$end = strpos($content,'</table>',$start) + 8;
$end = strpos($content,'<td valign="center"><div align="left" class="White8ptTxt_Bold">Social Kills</div></td>',$start) + 8;

$table = substr($content,$start,$end-$start);

preg_match_all("|<tr(.*)</tr>|U",$table,$rows);

foreach ($rows[0] as $row){

    if ((strpos($row,'<th')===false)){
   
        preg_match_all("|<td(.*)</td>|U",$row,$cells);
			     
        $title = strip_tags($cells[0][0]);
       
        $rank = strip_tags($cells[0][1]);
       
   $new_rank = preg_replace('/\s\(#\d+\)$/', '', $rank);

   
        $position = strip_tags($cells[0][2]);
         
	   
	 echo "{$title} - {$new_rank}<br>\n";
   
    }

}

?>

Link to comment
Share on other sites

When I echo just $rank I get this:

 

29 (#79889)

Captain, Grade 3 (#71621)

299  (#63007)

712 (#62321)

643 (#56351)

68 (#56111)

6646 (#62229)

6306 (#57047)

1.05 (#71229)

5802 (#40915)

5578 (#36691)

1.04

 

Here is my echo statement:

 

echo "{$rank}<br>\n";

 

Does it matter that I am using an Array?

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.