savj14 Posted April 3, 2008 Share Posted April 3, 2008 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 https://forums.phpfreaks.com/topic/99385-need-some-help-with-preg_match_all/ Share on other sites More sharing options...
Daniel0 Posted April 3, 2008 Share Posted April 3, 2008 Try this: <?php $var = 'Highest Skill - 29 (#79852)'; $new_var = preg_replace('/\s\(#\d+\)$/', '', $var); echo $new_var; ?> Link to comment https://forums.phpfreaks.com/topic/99385-need-some-help-with-preg_match_all/#findComment-508528 Share on other sites More sharing options...
savj14 Posted April 3, 2008 Author Share Posted April 3, 2008 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 https://forums.phpfreaks.com/topic/99385-need-some-help-with-preg_match_all/#findComment-508547 Share on other sites More sharing options...
savj14 Posted April 3, 2008 Author Share Posted April 3, 2008 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 https://forums.phpfreaks.com/topic/99385-need-some-help-with-preg_match_all/#findComment-508570 Share on other sites More sharing options...
Daniel0 Posted April 3, 2008 Share Posted April 3, 2008 Try to echo $rank before the preg_replace() to see if it contains the correct value. Link to comment https://forums.phpfreaks.com/topic/99385-need-some-help-with-preg_match_all/#findComment-508577 Share on other sites More sharing options...
savj14 Posted April 3, 2008 Author Share Posted April 3, 2008 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 https://forums.phpfreaks.com/topic/99385-need-some-help-with-preg_match_all/#findComment-508581 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.