mick104 Posted May 19, 2007 Share Posted May 19, 2007 I have the following script which scrapes a html table from a football fixtures page. This works fine and I have permission to use it. $source_file = file_get_contents('http://www.sportsmanager.ie/t3.php?report=1&reporttype=fixtures&sportid=1&club_id=&clubid=&nextweek=1&countyid=12&contentsportid=2 '); $arr_remove = array("\are", "\n", "\t", "\s"); $stripped_file = str_replace($arr_remove, '', $source_file); $strpos_start_section = strpos($stripped_file, '<table cellspacing="0" cellpadding="2" border="0" width="100%">'); $strpos_end_section = strpos($stripped_file, ' </table> ', $strpos_start_section) + 0; $len_section = $strpos_end_section - $strpos_start_section; $section = substr($stripped_file, $strpos_start_section, $len_section); echo $section; Returns this table <table border="0" cellpadding="2" cellspacing="0" width="100%"> <tbody> <tr class="reportrow"> <td colspan="7" class="reportrow_competition_name">Senior Hurling Championship Group A</td> </tr> <tr> <th class="reportrow_header">Team 1</th> <th class="reportrow_header">Team 2</th> <th class="reportrow_header">Venue</th> <th class="reportrow_header">Date</th> <th class="reportrow_header">Time</th> <th class="reportrow_header">Referee</th> <th class="reportrow_header">Comment</th> </tr> <tr class="reportrow"> <td>Sarsfields </td> <td>Gort </td> <td>Loughrea</td> <td>19/05/2007</td> <td>18:00</td> <td>Seamus Moran</td> <td>Comments for this match</td> </tr> </table> My question I would like to remove the width="100%" in the table tag and the last colum <th class="reportrow_header">Comment</th> with all the rows under this colum Any suggestions on how I would do this . Thanks Mick Quote Link to comment https://forums.phpfreaks.com/topic/52110-string-replace/ Share on other sites More sharing options...
chigley Posted May 19, 2007 Share Posted May 19, 2007 <?php $section = str_replace(" width=\"100%\"", "", $section); // Removes the width = "100%" from the table tag ?> I don't understand your second request though :S Quote Link to comment https://forums.phpfreaks.com/topic/52110-string-replace/#findComment-256972 Share on other sites More sharing options...
dark22horse Posted May 19, 2007 Share Posted May 19, 2007 Not sure if this is what you want <? $source_file = file_get_contents('http://www.sportsmanager.ie/t3.php?report=1&reporttype=fixtures&sportid=1&club_id=&clubid=&nextweek=1&countyid=12&contentsportid=2 '); $arr_remove = array("\are", "\n", "\t", "\s"); $stripped_file = str_replace($arr_remove, '', $source_file); $strpos_start_section = strpos($stripped_file, '<table cellspacing="0" cellpadding="2" border="0" width="100%">'); $strpos_end_section = strpos($stripped_file, ' </table> ', $strpos_start_section) + 0; $len_section = $strpos_end_section - $strpos_start_section; $section = substr($stripped_file, $strpos_start_section, $len_section); echo $section; ?> <html> <body> <table border="0" cellpadding="2" cellspacing="0" width="80%">//you can take this out, the whole width bit <tbody> <tr class="reportrow"> <td colspan="7" class="reportrow_competition_name">Senior Hurling Championship Group A</td> </tr> <tr> <th class="reportrow_header">Team 1</th> <th class="reportrow_header">Team 2</th> <th class="reportrow_header">Venue</th> <th class="reportrow_header">Date</th> <th class="reportrow_header">Time</th> <th class="reportrow_header">Referee</th> </tr> <tr class="reportrow"> <td>Sarsfields </td> <td>Gort </td> <td>Loughrea</td> <td>19/05/2007</td> <td>18:00</td> <td>Seamus Moran</td> </tr> </table> </html> </body> I have taken out the second one, if thats what you wanted? I have just tried it and it only brings the first one on the page. Quote Link to comment https://forums.phpfreaks.com/topic/52110-string-replace/#findComment-256973 Share on other sites More sharing options...
mick104 Posted May 19, 2007 Author Share Posted May 19, 2007 Thanks for the help chigley On the first question I want to remove the table width from the grabbed page. So where in my script will I place the $section = str_replace(" width=\"100%\"", "", $section); I already have a variable called section here is a link to the generated page http://www.westeirehosting.com/doc_stripper.php Q 2 I need the script to remove the colum comments from the returned page thanks again Mick Quote Link to comment https://forums.phpfreaks.com/topic/52110-string-replace/#findComment-256990 Share on other sites More sharing options...
mick104 Posted May 19, 2007 Author Share Posted May 19, 2007 Sorrry typo here $arr_remove = array("\are", "\n", "\t", "\s"); should be $arr_remove = array("\are", "\n", "\t", "\s"); I have tried to change are to the letter_r above but it reverts to are when i save it strange so are should read letter_r Quote Link to comment https://forums.phpfreaks.com/topic/52110-string-replace/#findComment-256991 Share on other sites More sharing options...
mick104 Posted May 19, 2007 Author Share Posted May 19, 2007 Ok got the first one $section = substr($stripped_file, $strpos_start_section, $len_section); $section = str_replace(" width=\"100%\"", "", $section); echo $section; Thanks for the help chigley Quote Link to comment https://forums.phpfreaks.com/topic/52110-string-replace/#findComment-256996 Share on other sites More sharing options...
kathas Posted May 19, 2007 Share Posted May 19, 2007 To remove the comment column do something like this...: First remove the heading: <?php $section = str_replace('<th class="reportrow_header">Comment</th>',"",$section); // here you could use a preg_replace too so that you don't care for class and style stuff only title ?> Then you need something like this that will remove the last column in the whole rest section: <?php $i = strrpos($section,'</tr>') while ($i !== FALSE) { $end = strrpos($section,'</td>',$i); $start = strrpos($section,'<td>',$end); $len = $end - $start + 3; $section = str_replace(substr($section,$start,$len),"",$section); $i = strrpos($section,'</tr>',$i-1); } ?> now u might want to check for "false" in $start and $end but i didn't... Btw this code is not tested i don't know if it is correct but the logic is this one... Kathas Quote Link to comment https://forums.phpfreaks.com/topic/52110-string-replace/#findComment-257003 Share on other sites More sharing options...
mick104 Posted May 19, 2007 Author Share Posted May 19, 2007 Thanks Kathas It will take a bit of time to get my head round this. One problem I might have is while the comment section is empty now it wont remain so. Thanks for the help I will test it on the server later. Mick Quote Link to comment https://forums.phpfreaks.com/topic/52110-string-replace/#findComment-257020 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.