Jump to content

string replace


mick104

Recommended Posts

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

 

 

 

 

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/52110-string-replace/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/52110-string-replace/#findComment-256973
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/52110-string-replace/#findComment-256990
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/52110-string-replace/#findComment-257003
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.