Jump to content

Parsing an entire html table


soltek

Recommended Posts

Hello again,

 

I'm trying to scrape a table from another website using preg_match, especifically, using this code:

<?php 
$data = file_get_contents('http://tvcountdown.com/index.php');
$regex = '/[color=red]<table class="episode_list_table">[/color] (.+?) [color=red]</table>[/color]/';
preg_match($regex,$data,$match);
var_dump($match); 
echo $match[0];
?>

 

Heres the thing. It doesnt work  :shy:

I think it's because the first and second anchors are html tags, 'cause if I parse some other stuff without any tag, there's no problemo.

 

Any hints, mates?

Thanks

Link to comment
Share on other sites

if you are actually looking for [ and ], you'll need to escape them since they are special reg-ex characters. the same goes for backslash, if you are using it as your open/close delimiters. so your regex should be more like

 

$regex = '/\[color=red\]<table class="episode_list_table">\[/color\] (.+?) \[color=red\]<\/table>\[\/color\]/';

Link to comment
Share on other sites

Oh I got it! Dumb me.

 

Now I'm not getting any warning, though, the array is still giving me nothing but «array(0) { } ».

I'm using this:

<?php 
$data = file_get_contents('http://tvcountdown.com/index.php');
$regex = '/<table class="episode_list_table" style="width:848px;margin-top:6px">  (.+?) <\/table>/';
preg_match($regex,$data,$match);
var_dump($match); 
echo $match[1];
?>

 

Maybe I'm doing something else wrong. I wanted to scrape evertything from <table class="episode_list_table" style="width:848px;margin-top:6px"> to </table>.

 

 

I'm seeing the source of http://tvcountdown.com/index.php and that data is there :s

<table class="episode_list_table" style="width:848px;margin-top:6px"> 
<tr> 
    <td class="c1 hr">Show</td> 
    <td class="c2 hr">Ep</td> 
    <td class="c3 hr">Title</td> 
    <td class="c4 hr">Countdown</td> 
    <td class="c5 hr" id="air_time_header"><div>Airtime (local)</div></td> 
[...]
<td class="c3"><a href="http://www.tvrage.com/Conan/episodes/1064999912">Jason Segel, Reggie Watts</a></td><td class="c4"><div id="c442_S01E28"></div></td><td class="c5"><div id="cc442_S01E28"></div></td></tr></table>

 

Link to comment
Share on other sites

Great, now 'I' got it parsed. Thanks!

 

Is there any way to do this? I'm getting an error:

 

[color=red]$url = http://tvcountdown.com/index.php;[/color]
preg_match('#<table class="episode_list_table"[^>]+>[\w\W]*?</table>#i', file_get_contents('[color=red]$url[/color]'), $match);

 

 

Link to comment
Share on other sites

What do you exactly want to do? Turn all text red in that table or what? Because then you have to go trough the whole table and make <td style="color: red"> or give them id="red" and then apply #red to your sites stylesheet.

 

But what do you want to do exactly? =)

Link to comment
Share on other sites

What do you exactly want to do? Turn all text red in that table or what? Because then you have to go trough the whole table and make <td style="color: red"> or give them id="red" and then apply #red to your sites stylesheet.

 

But what do you want to do exactly? =)

 

Sorry, I didnt noticed that BBcode couldnt be used inside the code tag.

The red parts were just to highlight what I intented to do.

 

 

$url = http://tvcountdown.com/index.php;
preg_match('#<table class="episode_list_table"[^>]+>[\w\W]*?</table>#i', file_get_contents('$url'), $match);

 

 

 

 

 

Link to comment
Share on other sites

Everything is working nicely, but I wanted to change this part:

 

preg_match('#<table class="episode_list_table"[^>]+>[\w\W]*?</table>#i', file_get_contents('http://tvcountdown.com/index.php'), $match);

 

to something like this:

 

$url = http://tvcountdown.com/index.php;

preg_match('#<table class="episode_list_table"[^>]+>[\w\W]*?</table>#i', file_get_contents('$url'), $match);

 

 

I tried that, but it didnt work very well  :shrug:

Thanks for passing by.

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.