By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,350 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,350 IT Pros & Developers. It's quick & easy.

how to extract part of HTML page

P: n/a
Hi everyone,

I'm trying to write a PHP script that connects to a bank's currency
convertor page using cURL and that part works fine. The issue is that I end
up with a page that includes a lot of information that I don't need. Using
the PHP function strip_tags I've ended with the text below and from the
remaining HTML code, I'd like to extract the lines starting with "<TABLE
BORDER="1" WIDTH="315">" up to its closing </TABLEtag. How do I do this
using PHP? I tried using preg_match and the like but my regex skills are
pretty bad. Not too sure where to start. Could someone please provide me
with some pointers?
================================================== =======================================
<TABLE BORDER="0" WIDTH="600">
<tr>
<td width="148"></td>
<td width="448">some text some text some text some text some text</td>
</tr>
</TABLE>

<TABLE BORDER="0" WIDTH="600">
<TR><TD VALIGN="top" WIDTH="148">
</TD>
<TD WIDTH="448" VALIGN="top">
<TABLE BORDER="0" WIDTH="448">
<TR><TD>
some text some text some text some text some text some text some text some
text some text some text.
some text some text some text some text some text
</TD></TR>
<TR><TD>
<TABLE BORDER="1" WIDTH="315" <----- extract
from here

<TR><TD>
some text some text some text some text some
text
</TD>
<TD ALIGN="right">
some text some text some text some text some
text
</TD></TR>
<TR><TD>
some text some text some text some text some
text
</TD>

<TD ALIGN="right">
some text some text some text some text some
text
</TD></TR>
<TR><TD>
some text some text some text some text some
text
</TD>
<TD ALIGN="right">
some text some text some text some text some
text
</TD></TR>
</TABLE>
<--------- to here
</TD></TR>
<TR><TD>
a {
color:blue;
}
some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text
</TD></TR>
<TR><TD>
some text some text some text some text some text
some text some text some text some text some text some text
some text some text some text some text some text some
</TD></TR>
</TABLE>
</TD></TR>
</TABLE>
<br>
================================================== =======================================
Thanks
Eric

Sep 16 '08 #1
Share this Question
Share on Google+
18 Replies


P: n/a
On Tue, 16 Sep 2008 21:42:20 +1000, Ecka wrote:
I'd like to extract the lines starting
with "<TABLE BORDER="1" WIDTH="315">" up to its closing </TABLEtag.
With DOMDocument::loadHTML(), you can simply do an XPath query on the
HTML.

An alternative is to split the HTML in lines, loop through the lines and
put all the lines between <tableand </tablein a string:

$lines = explode('\n', $text);
foreach ($lines as $line) {
if (preg_match('~<table~', $line)) {
$on = true;
} else if (preg_match('~</table>~', $line)) {
$on = false;
}
if ($on) $result .= $line;
}
echo $result;
Sep 16 '08 #2

P: n/a
r0g
Ecka wrote:
Hi everyone,

I'm trying to write a PHP script that connects to a bank's currency
convertor page using cURL and that part works fine. The issue is that I
end up with a page that includes a lot of information that I don't
need. Using the PHP function strip_tags I've ended with the text below
and from the remaining HTML code, I'd like to extract the lines starting
with "<TABLE BORDER="1" WIDTH="315">" up to its closing </TABLEtag.
How do I do this using PHP? I tried using preg_match and the like but
my regex skills are pretty bad. Not too sure where to start. Could
someone please provide me with some pointers?
================================================== =======================================

<TABLE BORDER="0" WIDTH="600">
<tr>
<td width="148"></td>
<td width="448">some text some text some text some text some text</td>
</tr>
</TABLE>

<TABLE BORDER="0" WIDTH="600">
<TR><TD VALIGN="top" WIDTH="148">
</TD>
<TD WIDTH="448" VALIGN="top">
<TABLE BORDER="0" WIDTH="448">
<TR><TD>
some text some text some text some text some text some text some text
some text some text some text.
some text some text some text some text some text
</TD></TR>
<TR><TD>
<TABLE BORDER="1" WIDTH="315" <-----
extract from here

<TR><TD>
some text some text some text some text
some text
</TD>
<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
<TR><TD>
some text some text some text some text
some text
</TD>

<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
<TR><TD>
some text some text some text some text
some text
</TD>
<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
</TABLE<--------- to here
</TD></TR>
<TR><TD>
a {
color:blue;
}
some text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text
</TD></TR>
<TR><TD>
some text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text some text some
</TD></TR>
</TABLE>
</TD></TR>
</TABLE>
<br>
================================================== =======================================

Thanks
Eric
Try this.

preg_match('#TABLE BORDER="1"
WIDTH="315">(.*?)?\</TABLE>#s',$source,$result);
print $result[1];

Always start small with regexes and build them up a bit at a time, if
you try and do a complex one all in one go you're asking for trouble!

Regards,

Roger.
Sep 16 '08 #3

P: n/a
r0g wrote:
Ecka wrote:
>Hi everyone,

I'm trying to write a PHP script that connects to a bank's currency
convertor page using cURL and that part works fine. The issue is that I
end up with a page that includes a lot of information that I don't
need. Using the PHP function strip_tags I've ended with the text below
and from the remaining HTML code, I'd like to extract the lines starting
with "<TABLE BORDER="1" WIDTH="315">" up to its closing </TABLEtag.
How do I do this using PHP? I tried using preg_match and the like but
my regex skills are pretty bad. Not too sure where to start. Could
someone please provide me with some pointers?
================================================= ========================================

<TABLE BORDER="0" WIDTH="600">
<tr>
<td width="148"></td>
<td width="448">some text some text some text some text some text</td>
</tr>
</TABLE>

<TABLE BORDER="0" WIDTH="600">
<TR><TD VALIGN="top" WIDTH="148">
</TD>
<TD WIDTH="448" VALIGN="top">
<TABLE BORDER="0" WIDTH="448">
<TR><TD>
some text some text some text some text some text some text some text
some text some text some text.
some text some text some text some text some text
</TD></TR>
<TR><TD>
<TABLE BORDER="1" WIDTH="315" <-----
extract from here

<TR><TD>
some text some text some text some text
some text
</TD>
<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
<TR><TD>
some text some text some text some text
some text
</TD>

<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
<TR><TD>
some text some text some text some text
some text
</TD>
<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
</TABLE<--------- to here
</TD></TR>
<TR><TD>
a {
color:blue;
}
some text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text
</TD></TR>
<TR><TD>
some text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text some text some
</TD></TR>
</TABLE>
</TD></TR>
</TABLE>
<br>
================================================= ========================================

Thanks
Eric

Try this.

preg_match('#TABLE BORDER="1"
WIDTH="315">(.*?)?\</TABLE>#s',$source,$result);
print $result[1];
You forgot the opening "<" for the table tag. You also have a 0-1
quantifier after the first capture group, and the OP doesn't seem to
want an empty table. You have an unnecessary escape of "<" for the end
table tag.

@OP: unfortunately, this approach relies on the site authors not
changing their code. We can further minimize hard coding by using the
pre-defined space character class, and case insensitivity. The /x
modifier let's your regex ignore space and easily insert comments.

preg_match('~
<table \s+ # open table tag

# attributes
border=[\'"]1[\'"] \s+
width=[\'"]315[\'"]>

(.*?) # table body

</table# end table tag
~sxi',
$source,
$match_array
);
Always start small with regexes and build them up a bit at a time, if
you try and do a complex one all in one go you're asking for trouble!
Very good advice.
Regards,

Roger.
--
Curtis
Sep 16 '08 #4

P: n/a
r0g
Curtis wrote:
r0g wrote:
>Ecka wrote:
>>Hi everyone,

I'm trying to write a PHP script that connects to a bank's currency
convertor page using cURL and that part works fine. The issue is that I
end up with a page that includes a lot of information that I don't
need. Using the PHP function strip_tags I've ended with the text below
and from the remaining HTML code, I'd like to extract the lines starting
with "<TABLE BORDER="1" WIDTH="315">" up to its closing </TABLEtag.
How do I do this using PHP? I tried using preg_match and the like but
my regex skills are pretty bad. Not too sure where to start. Could
someone please provide me with some pointers?
================================================ =========================================
<TABLE BORDER="0" WIDTH="600">
<tr>
<td width="148"></td>
<td width="448">some text some text some text some text some text</td>
</tr>
</TABLE>

<TABLE BORDER="0" WIDTH="600">
<TR><TD VALIGN="top" WIDTH="148">
</TD>
<TD WIDTH="448" VALIGN="top">
<TABLE BORDER="0" WIDTH="448">
<TR><TD>
some text some text some text some text some text some text some text
some text some text some text.
some text some text some text some text some text
</TD></TR>
<TR><TD>
<TABLE BORDER="1" WIDTH="315" <-----
extract from here

<TR><TD>
some text some text some text some text
some text
</TD>
<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
<TR><TD>
some text some text some text some text
some text
</TD>

<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
<TR><TD>
some text some text some text some text
some text
</TD>
<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
</TABLE<--------- to here
</TD></TR>
<TR><TD>
a {
color:blue;
}
some text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text
</TD></TR>
<TR><TD>
some text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text some text some
</TD></TR>
</TABLE>
</TD></TR>
</TABLE>
<br>
================================================ =========================================
Thanks
Eric

Try this.

preg_match('#TABLE BORDER="1"
WIDTH="315">(.*?)?\</TABLE>#s',$source,$result);
print $result[1];

You forgot the opening "<" for the table tag. You also have a 0-1
quantifier after the first capture group, and the OP doesn't seem to
want an empty table. You have an unnecessary escape of "<" for the end
table tag.

@OP: unfortunately, this approach relies on the site authors not
changing their code. We can further minimize hard coding by using the
pre-defined space character class, and case insensitivity. The /x
modifier let's your regex ignore space and easily insert comments.

preg_match('~
<table \s+ # open table tag

# attributes
border=[\'"]1[\'"] \s+
width=[\'"]315[\'"]>

(.*?) # table body

</table# end table tag
~sxi',
$source,
$match_array
);
>Always start small with regexes and build them up a bit at a time, if
you try and do a complex one all in one go you're asking for trouble!

Very good advice.
>Regards,

Roger.

True enough, twas pretty slapdash, even for a usenet post ;-) For some
reason earlier my copy of php was choking on those left angle brackets
and I didn't want to spend the time figuring out why. Oddly enough
though I've just tried it with the angle brackets back in again and it
works fine now, go figure eh,! The surplus quantifier was a remnant from
an earlier attempt to step around the left bracket thing too.

Bloody regexes! I love them dearly but Zawinski had a point! :-)

Roger.
Sep 16 '08 #5

P: n/a

"r0g" <ai******@technicalbloke.comwrote in message
news:ga**********@aioe.org...
Curtis wrote:
>r0g wrote:
<snip>
>>>>

Try this.

preg_match('#TABLE BORDER="1"
WIDTH="315">(.*?)?\</TABLE>#s',$source,$result);
print $result[1];

You forgot the opening "<" for the table tag. You also have a 0-1
quantifier after the first capture group, and the OP doesn't seem to
want an empty table. You have an unnecessary escape of "<" for the end
table tag.

@OP: unfortunately, this approach relies on the site authors not
changing their code. We can further minimize hard coding by using the
pre-defined space character class, and case insensitivity. The /x
modifier let's your regex ignore space and easily insert comments.

preg_match('~
<table \s+ # open table tag

# attributes
border=[\'"]1[\'"] \s+
width=[\'"]315[\'"]>

(.*?) # table body

</table# end table tag
~sxi',
$source,
$match_array
);
>>Always start small with regexes and build them up a bit at a time, if
you try and do a complex one all in one go you're asking for trouble!

Very good advice.
>>Regards,

Roger.


True enough, twas pretty slapdash, even for a usenet post ;-) For some
reason earlier my copy of php was choking on those left angle brackets
and I didn't want to spend the time figuring out why. Oddly enough
though I've just tried it with the angle brackets back in again and it
works fine now, go figure eh,! The surplus quantifier was a remnant from
an earlier attempt to step around the left bracket thing too.

Bloody regexes! I love them dearly but Zawinski had a point! :-)

Roger.
Thanks guys!

Will give them a try and report back.

Cheers,
Eric

Sep 16 '08 #6

P: n/a
r0g wrote:
Curtis wrote:
>r0g wrote:
>>Ecka wrote:
Hi everyone,

I'm trying to write a PHP script that connects to a bank's currency
convertor page using cURL and that part works fine. The issue is that I
end up with a page that includes a lot of information that I don't
need. Using the PHP function strip_tags I've ended with the text below
and from the remaining HTML code, I'd like to extract the lines starting
with "<TABLE BORDER="1" WIDTH="315">" up to its closing </TABLEtag.
How do I do this using PHP? I tried using preg_match and the like but
my regex skills are pretty bad. Not too sure where to start. Could
someone please provide me with some pointers?
=============================================== ==========================================
<TABLE BORDER="0" WIDTH="600">
<tr>
<td width="148"></td>
<td width="448">some text some text some text some text some text</td>
</tr>
</TABLE>

<TABLE BORDER="0" WIDTH="600">
<TR><TD VALIGN="top" WIDTH="148">
</TD>
<TD WIDTH="448" VALIGN="top">
<TABLE BORDER="0" WIDTH="448">
<TR><TD>
some text some text some text some text some text some text some text
some text some text some text.
some text some text some text some text some text
</TD></TR>
<TR><TD>
<TABLE BORDER="1" WIDTH="315" <-----
extract from here

<TR><TD>
some text some text some text some text
some text
</TD>
<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
<TR><TD>
some text some text some text some text
some text
</TD>

<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
<TR><TD>
some text some text some text some text
some text
</TD>
<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
</TABLE<--------- to here
</TD></TR>
<TR><TD>
a {
color:blue;
}
some text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text
</TD></TR>
<TR><TD>
some text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text some text some
</TD></TR>
</TABLE>
</TD></TR>
</TABLE>
<br>
=============================================== ==========================================
Thanks
Eric

Try this.

preg_match('#TABLE BORDER="1"
WIDTH="315">(.*?)?\</TABLE>#s',$source,$result);
print $result[1];
You forgot the opening "<" for the table tag. You also have a 0-1
quantifier after the first capture group, and the OP doesn't seem to
want an empty table. You have an unnecessary escape of "<" for the end
table tag.

@OP: unfortunately, this approach relies on the site authors not
changing their code. We can further minimize hard coding by using the
pre-defined space character class, and case insensitivity. The /x
modifier let's your regex ignore space and easily insert comments.

preg_match('~
<table \s+ # open table tag

# attributes
border=[\'"]1[\'"] \s+
width=[\'"]315[\'"]>

(.*?) # table body

</table# end table tag
~sxi',
$source,
$match_array
);
>>Always start small with regexes and build them up a bit at a time, if
you try and do a complex one all in one go you're asking for trouble!
Very good advice.
>>Regards,

Roger.


True enough, twas pretty slapdash, even for a usenet post ;-) For some
reason earlier my copy of php was choking on those left angle brackets
and I didn't want to spend the time figuring out why. Oddly enough
though I've just tried it with the angle brackets back in again and it
works fine now, go figure eh,! The surplus quantifier was a remnant from
an earlier attempt to step around the left bracket thing too.

Bloody regexes! I love them dearly but Zawinski had a point! :-)

Roger.
I struggled with regex for a long time, but now I love them as well.
Sorry if I came off as too pedantic, and yes, I use a quote from
Zawnski (maybe the same to which you referred) elsewhere. :-)

--
Curtis
Sep 16 '08 #7

P: n/a
Ecka wrote:
Hi everyone,

I'm trying to write a PHP script that connects to a bank's currency
convertor page using cURL and that part works fine. The issue is that I
end up with a page that includes a lot of information that I don't
need. Using the PHP function strip_tags I've ended with the text below
and from the remaining HTML code, I'd like to extract the lines starting
with "<TABLE BORDER="1" WIDTH="315">" up to its closing </TABLEtag.
How do I do this using PHP? I tried using preg_match and the like but
my regex skills are pretty bad. Not too sure where to start. Could
someone please provide me with some pointers?
================================================== =======================================

<TABLE BORDER="0" WIDTH="600">
<tr>
<td width="148"></td>
<td width="448">some text some text some text some text some text</td>
</tr>
</TABLE>

<TABLE BORDER="0" WIDTH="600">
<TR><TD VALIGN="top" WIDTH="148">
</TD>
<TD WIDTH="448" VALIGN="top">
<TABLE BORDER="0" WIDTH="448">
<TR><TD>
some text some text some text some text some text some text some text
some text some text some text.
some text some text some text some text some text
</TD></TR>
<TR><TD>
<TABLE BORDER="1" WIDTH="315" <-----
extract from here

<TR><TD>
some text some text some text some text
some text
</TD>
<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
<TR><TD>
some text some text some text some text
some text
</TD>

<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
<TR><TD>
some text some text some text some text
some text
</TD>
<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
</TABLE<--------- to here
</TD></TR>
<TR><TD>
a {
color:blue;
}
some text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text
</TD></TR>
<TR><TD>
some text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text some text some
</TD></TR>
</TABLE>
</TD></TR>
</TABLE>
<br>
================================================== =======================================

Thanks
Eric

Hmmm, to me a regex seems a bit overkill here. There's a lot of
overhead with regex's.

How about something like:

$start = strpos($rawdata, '<TABLE BORDER="1" WIDTH="315">');
if ($start === false)
echo 'Start not found';
else {
$stop = strpos($text, '</TABLE>', $start);
if ($stop === false)
echo 'End not found';
else {
$text = substr($rawdata, $start + 31, $stop - $start - 31);
}

It's longer, but should have less overhead than a regex.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 16 '08 #8

P: n/a
Jerry Stuckle wrote:
Ecka wrote:
>Hi everyone,

I'm trying to write a PHP script that connects to a bank's currency
convertor page using cURL and that part works fine. The issue is that
I end up with a page that includes a lot of information that I don't
need. Using the PHP function strip_tags I've ended with the text
below and from the remaining HTML code, I'd like to extract the lines
starting with "<TABLE BORDER="1" WIDTH="315">" up to its closing
</TABLEtag. How do I do this using PHP? I tried using preg_match
and the like but my regex skills are pretty bad. Not too sure where
to start. Could someone please provide me with some pointers?
================================================= ========================================

<TABLE BORDER="0" WIDTH="600">
<tr>
<td width="148"></td>
<td width="448">some text some text some text some text some text</td>
</tr>
</TABLE>

<TABLE BORDER="0" WIDTH="600">
<TR><TD VALIGN="top" WIDTH="148">
</TD>
<TD WIDTH="448" VALIGN="top">
<TABLE BORDER="0" WIDTH="448">
<TR><TD>
some text some text some text some text some text some text some text
some text some text some text.
some text some text some text some text some text
</TD></TR>
<TR><TD>
<TABLE BORDER="1" WIDTH="315" <-----
extract from here

<TR><TD>
some text some text some text some text
some text
</TD>
<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
<TR><TD>
some text some text some text some text
some text
</TD>

<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
<TR><TD>
some text some text some text some text
some text
</TD>
<TD ALIGN="right">
some text some text some text some text
some text
</TD></TR>
</TABLE<--------- to here
</TD></TR>
<TR><TD>
a {
color:blue;
}
some text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text
</TD></TR>
<TR><TD>
some text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text some text some
</TD></TR>
</TABLE>
</TD></TR>
</TABLE>
<br>
================================================= ========================================

Thanks
Eric


Hmmm, to me a regex seems a bit overkill here. There's a lot of
overhead with regex's.

How about something like:

$start = strpos($rawdata, '<TABLE BORDER="1" WIDTH="315">');
if ($start === false)
echo 'Start not found';
else {
$stop = strpos($text, '</TABLE>', $start);
if ($stop === false)
echo 'End not found';
else {
$text = substr($rawdata, $start + 31, $stop - $start - 31);
}

It's longer, but should have less overhead than a regex.
Jerry, I couldn't get your code to work, I had to make a few changes

$text = '';
$needle = '<TABLE BORDER="1" WIDTH="315">';
$start = strpos($rawdata, $needle);

if ($start === false)
echo 'Start not found';
else {
// first argument is $rawdata, not $text
$stop = strpos($rawdata, '</TABLE>', $start);

if ($stop === false)
echo 'End not found';

// removed extra { with no matching end }
else
$text = substr($rawdata, $start + strlen($needle), $stop - $start
- strlen($needle));
}

Changing to strlen makes it clearer than hard coding the string length
of the needle.

--
Curtis
Sep 16 '08 #9

P: n/a
Curtis wrote:
Jerry Stuckle wrote:
>Ecka wrote:
>>Hi everyone,

I'm trying to write a PHP script that connects to a bank's currency
convertor page using cURL and that part works fine. The issue is
that I end up with a page that includes a lot of information that I
don't need. Using the PHP function strip_tags I've ended with the
text below and from the remaining HTML code, I'd like to extract the
lines starting with "<TABLE BORDER="1" WIDTH="315">" up to its
closing </TABLEtag. How do I do this using PHP? I tried using
preg_match and the like but my regex skills are pretty bad. Not too
sure where to start. Could someone please provide me with some
pointers?
================================================ =========================================

<TABLE BORDER="0" WIDTH="600">
<tr>
<td width="148"></td>
<td width="448">some text some text some text some text some text</td>
</tr>
</TABLE>

<TABLE BORDER="0" WIDTH="600">
<TR><TD VALIGN="top" WIDTH="148">
</TD>
<TD WIDTH="448" VALIGN="top">
<TABLE BORDER="0" WIDTH="448">
<TR><TD>
some text some text some text some text some text some text some text
some text some text some text.
some text some text some text some text some text
</TD></TR>
<TR><TD>
<TABLE BORDER="1" WIDTH="315" <-----
extract from here

<TR><TD>
some text some text some text some
text some text
</TD>
<TD ALIGN="right">
some text some text some text some
text some text
</TD></TR>
<TR><TD>
some text some text some text some
text some text
</TD>

<TD ALIGN="right">
some text some text some text some
text some text
</TD></TR>
<TR><TD>
some text some text some text some
text some text
</TD>
<TD ALIGN="right">
some text some text some text some
text some text
</TD></TR>
</TABLE<--------- to here
</TD></TR>
<TR><TD>
a {
color:blue;
}
some text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text
</TD></TR>
<TR><TD>
some text some text some text some text some text
some text some text some text some text some text some
text some text some text some text some text some text some
</TD></TR>
</TABLE>
</TD></TR>
</TABLE>
<br>
================================================ =========================================

Thanks
Eric


Hmmm, to me a regex seems a bit overkill here. There's a lot of
overhead with regex's.

How about something like:

$start = strpos($rawdata, '<TABLE BORDER="1" WIDTH="315">');
if ($start === false)
echo 'Start not found';
else {
$stop = strpos($text, '</TABLE>', $start);
if ($stop === false)
echo 'End not found';
else {
$text = substr($rawdata, $start + 31, $stop - $start - 31);
}

It's longer, but should have less overhead than a regex.

Jerry, I couldn't get your code to work, I had to make a few changes

$text = '';
$needle = '<TABLE BORDER="1" WIDTH="315">';
$start = strpos($rawdata, $needle);

if ($start === false)
echo 'Start not found';
else {
// first argument is $rawdata, not $text
$stop = strpos($rawdata, '</TABLE>', $start);

if ($stop === false)
echo 'End not found';

// removed extra { with no matching end }
else
$text = substr($rawdata, $start + strlen($needle), $stop - $start -
strlen($needle));
}

Changing to strlen makes it clearer than hard coding the string length
of the needle.
You're right - I got the wrong parameter there - comes from changing
variable names in mid stream :-)

The reason I didn't use strlen was to try to cut down the overhead. I
just don't like using strlen with fixed length strings. Personal
preference, mostly.

In the real code I would have just added a comment indicating what the
31 was.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 17 '08 #10

P: n/a
On Tue, 16 Sep 2008 18:14:31 -0400, Jerry Stuckle wrote:
Ecka wrote:
Hmmm, to me a regex seems a bit overkill here. There's a lot of
overhead with regex's.
A preg_match() such as those suggested take on my system approx 93
microseconds. Will the user notice that? Is it worth the hassle to
rewrite it to use strpos() and substr(), even though that may be several
times faster?
Sep 17 '08 #11

P: n/a
r0g
Sjoerd wrote:
On Tue, 16 Sep 2008 18:14:31 -0400, Jerry Stuckle wrote:
>Ecka wrote:
Hmmm, to me a regex seems a bit overkill here. There's a lot of
overhead with regex's.

A preg_match() such as those suggested take on my system approx 93
microseconds. Will the user notice that? Is it worth the hassle to
rewrite it to use strpos() and substr(), even though that may be several
times faster?
Well I guess it depends how far and wide and often your man plans on
scraping! For google scale operations things like this are a significant
factor, although I have to agree chances are it's going to be pretty
insignificant in this case :]

Roger.
Sep 17 '08 #12

P: n/a
Sjoerd wrote:
On Tue, 16 Sep 2008 18:14:31 -0400, Jerry Stuckle wrote:
>Ecka wrote:
Hmmm, to me a regex seems a bit overkill here. There's a lot of
overhead with regex's.

A preg_match() such as those suggested take on my system approx 93
microseconds. Will the user notice that? Is it worth the hassle to
rewrite it to use strpos() and substr(), even though that may be several
times faster?
YMMV. I wouldn't have spent the time trying to figure out a regex in
the first place. Regex's are great for a lot of things. But sometimes
simplicity rules. And I find strpos() and strlen() to be much easier to
understand than a regex.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 17 '08 #13

P: n/a
Response to Jerry Stuckle <js*******@attglobal.net>:
>A preg_match() such as those suggested take on my system approx
93 microseconds. Will the user notice that? Is it worth the
hassle to rewrite it to use strpos() and substr(), even though
that may be several times faster?
Did you run it 100,000 times? Did you run it 100,000 times
concurrently?

I think that is where the primary concern for speed should be
addressed.
YMMV. I wouldn't have spent the time trying to figure out a
regex in the first place. Regex's are great for a lot of
things. But sometimes simplicity rules. And I find strpos()
and strlen() to be much easier to understand than a regex.
Not to mention the simplicity involved if/when the markup provider
(a bank in this case) changes the content. A major change in
format could make a regex a painful process whereby the string
routines will be a "cut & paste" exercise.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Sep 17 '08 #14

P: n/a
-Lost wrote:
Response to Jerry Stuckle <js*******@attglobal.net>:
>>A preg_match() such as those suggested take on my system approx
93 microseconds. Will the user notice that? Is it worth the
hassle to rewrite it to use strpos() and substr(), even though
that may be several times faster?

Did you run it 100,000 times? Did you run it 100,000 times
concurrently?
Please quote properly when you are replying.
I think that is where the primary concern for speed should be
addressed.
>YMMV. I wouldn't have spent the time trying to figure out a
regex in the first place. Regex's are great for a lot of
things. But sometimes simplicity rules. And I find strpos()
and strlen() to be much easier to understand than a regex.

Not to mention the simplicity involved if/when the markup provider
(a bank in this case) changes the content. A major change in
format could make a regex a painful process whereby the string
routines will be a "cut & paste" exercise.
True - but ANY change in the input can cause these problems.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 18 '08 #15

P: n/a
r0g
-Lost wrote:
Response to Jerry Stuckle <js*******@attglobal.net>:
>>A preg_match() such as those suggested take on my system approx
93 microseconds. Will the user notice that? Is it worth the
hassle to rewrite it to use strpos() and substr(), even though
that may be several times faster?

Did you run it 100,000 times? Did you run it 100,000 times
concurrently?
That would take about 9.5 seconds by my reckoning. Downloading a 2kb
html file 100000 times = 200Mb and would take you 3.5 minutes on a very
good day on a 10Mbit line not accounting for DNS lookups and other
sources of lag, nor indeed for the website he's scraping noticing the
flood of requests and banning his IP.
I think that is where the primary concern for speed should be
addressed.
>YMMV. I wouldn't have spent the time trying to figure out a
regex in the first place. Regex's are great for a lot of
things. But sometimes simplicity rules. And I find strpos()
and strlen() to be much easier to understand than a regex.

Not to mention the simplicity involved if/when the markup provider
(a bank in this case) changes the content. A major change in
format could make a regex a painful process whereby the string
routines will be a "cut & paste" exercise.
TBH, neither would be that bad a ballache. Regex would have more chance
of adapting to them taking anti scraping measures and salting their html
though.

TBH, execution time is far less important than developer time most of
the time, whatever's fastest to code and maintain is the right answer
and that will be different from developer to developer.

Roger.
Sep 18 '08 #16

P: n/a
On Sep 18, 12:20*am, r0g <aioe....@technicalbloke.comwrote:
-Lost wrote:
Response to Jerry Stuckle <jstuck...@attglobal.net>:
>A preg_match() such as those suggested take on my system approx
93 microseconds. Will the user notice that? Is it worth the
hassle to rewrite it to use strpos() and substr(), even though
that may be several times faster?
Did you run it 100,000 times? *Did you run it 100,000 times
concurrently?

That would take about 9.5 seconds by my reckoning. Downloading a 2kb
html file 100000 times = 200Mb and would take you 3.5 minutes on a very
good day on a 10Mbit line not accounting for DNS lookups and other
sources of lag, nor indeed for the website he's scraping noticing the
flood of requests and banning his IP.
I think that is where the primary concern for speed should be
addressed.
YMMV. *I wouldn't have spent the time trying to figure out a
regex in the first place. *Regex's are great for a lot of
things. *But sometimes simplicity rules. *And I find strpos()
and strlen() to be much easier to understand than a regex.
Not to mention the simplicity involved if/when the markup provider
(a bank in this case) changes the content. *A major change in
format could make a regex a painful process whereby the string
routines will be a "cut & paste" exercise.

TBH, neither would be that bad a ballache. Regex would have more chance
of adapting to them taking anti scraping measures and salting their html
though.

TBH, execution time is far less important than developer time most of
the time, whatever's fastest to code and maintain is the right answer
and that will be different from developer to developer.

Roger.
Jerry has a point on simplifying the code.

There has been many times in the past where I wrote some convoluted /
intricate logic that worked great, but when I returned to it a month
later the meaning behind it was not obviously clear and it would take
some time to understand exactly what I was doing. Commenting can help,
but writing 5 lines of comment to describe how one line of code works
seems overkill.

Now I break down involved logic into smaller parts so that I (or
someone else) will be able to understand what is happening at a glance
and be able to make changes to it without having to decipher what is
happening.

Bill H
Sep 18 '08 #17

P: n/a
Message-ID:
<5c**********************************@d77g2000hsb. googlegroups.comfrom
Bill H contained the following:
>There has been many times in the past where I wrote some convoluted /
intricate logic that worked great, but when I returned to it a month
later the meaning behind it was not obviously clear and it would take
some time to understand exactly what I was doing.
BTDTGTTS
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk - http://4theweb.co.uk
Sep 18 '08 #18

P: n/a
On 16 Sep, 13:09, Sjoerd <sjoer...@gmail.comwrote:
On Tue, 16 Sep 2008 21:42:20 +1000, Ecka wrote:
I'd like to extract the lines starting
with "<TABLE BORDER="1" WIDTH="315">" up to its closing </TABLEtag.

With DOMDocument::loadHTML(), you can simply do an XPath query on the
HTML.

An alternative is to split the HTML in lines, loop through the lines and
put all the lines between <tableand </tablein a string:

$lines = explode('\n', $text);
foreach ($lines as $line) {
if (preg_match('~<table~', $line)) {
$on = true;
} else if (preg_match('~</table>~', $line)) {
$on = false;
}
if ($on) $result .= $line;}

echo $result;
Or parse it as an XML file.

C.
Sep 19 '08 #19

This discussion thread is closed

Replies have been disabled for this discussion.