473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

how to extract part of HTML page

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
18 3937
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
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
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
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

"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
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
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
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
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
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
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
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
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
-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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Logical | last post by:
I wanted to do: include('page.htm?id=12&foo=bar'); But since I can't (and don't want to make another seperate HTTP request with include('http://...')); I was wondering if there's a function...
4
by: David Chang | last post by:
I am trying to do some analysis on customer's locality, so I'd like to extract numeric part from address. Here is how the table looks like Table Member first_name last_name address1 ...
2
by: Kevin Morgan | last post by:
I'm trying to write a widget for Mac OSX Tiger. Here's the problem: The user enters a search term which is sent to a perl script on a remote server. This script returns a fully formatted HTML page....
4
by: jrefactors | last post by:
How to extract data from html page? For example, if i want to get the information of weather (http://weather.yahoo.com/forecast/USCA1005.html) and put in my web page. Is it possible to do that? ...
3
by: Joe | last post by:
I'm trying to extract part of html code from a tag to a tag code begins with <span class="boldyellow"><B><U> and ends with TD><TD> <img src="http://whatever/some.gif"> </TD></TR></TABLE> I was...
7
by: A Causal | last post by:
I'm an experienced C programmer, but I have never worked with any sort of internet programming. I would like to write a program to search for certain character strings in a currently displayed web...
3
by: Robot | last post by:
Dear all, I need to create a script which will extract the contents of 2 cells of an html that contains a specified number of cells.Then I need to put that contents in another cells of my own html...
7
by: Ulysse | last post by:
Hello, I'm trying to extract the data from HTML table. Here is the part of the HTML source : """ <tr> <td class="tdn" valign="top"> <input name="x44553130" value="y" type="checkbox"></td>...
9
by: flit | last post by:
Hello All, Using poplib in python I can extract only the headers using the .top, there is a way to extract only the message text without the headers? like remove the fields below: "...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.