first global commit
This commit is contained in:
@@ -0,0 +1,274 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc. 675 Mass Ave,
|
||||
Cambridge, MA 02139, USA. Everyone is permitted to copy and distribute
|
||||
verbatim copies of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your freedom to
|
||||
share and change it. By contrast, the GNU General Public License is
|
||||
intended to guarantee your freedom to share and change free software--to
|
||||
make sure the software is free for all its users. This General Public License
|
||||
applies to most of the Free Software Foundation's software and to any other
|
||||
program whose authors commit to using it. (Some other Free Software
|
||||
Foundation software is covered by the GNU Library General Public License
|
||||
instead.) You can apply it to your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not price. Our
|
||||
General Public Licenses are designed to make sure that you have the
|
||||
freedom to distribute copies of free software (and charge for this service if
|
||||
you wish), that you receive source code or can get it if you want it, that you
|
||||
can change the software or use pieces of it in new free programs; and that
|
||||
you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid anyone to
|
||||
deny you these rights or to ask you to surrender the rights. These restrictions
|
||||
translate to certain responsibilities for you if you distribute copies of the
|
||||
software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether gratis or for
|
||||
a fee, you must give the recipients all the rights that you have. You must make
|
||||
sure that they, too, receive or can get the source code. And you must show
|
||||
them these terms so they know their rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and (2)
|
||||
offer you this license which gives you legal permission to copy, distribute
|
||||
and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain that
|
||||
everyone understands that there is no warranty for this free software. If the
|
||||
software is modified by someone else and passed on, we want its recipients
|
||||
to know that what they have is not the original, so that any problems
|
||||
introduced by others will not reflect on the original authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software patents. We
|
||||
wish to avoid the danger that redistributors of a free program will individually
|
||||
obtain patent licenses, in effect making the program proprietary. To prevent
|
||||
this, we have made it clear that any patent must be licensed for everyone's
|
||||
free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and modification
|
||||
follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND
|
||||
MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains a notice
|
||||
placed by the copyright holder saying it may be distributed under the terms
|
||||
of this General Public License. The "Program", below, refers to any such
|
||||
program or work, and a "work based on the Program" means either the
|
||||
Program or any derivative work under copyright law: that is to say, a work
|
||||
containing the Program or a portion of it, either verbatim or with
|
||||
modifications and/or translated into another language. (Hereinafter, translation
|
||||
is included without limitation in the term "modification".) Each licensee is
|
||||
addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not covered
|
||||
by this License; they are outside its scope. The act of running the Program is
|
||||
not restricted, and the output from the Program is covered only if its contents
|
||||
constitute a work based on the Program (independent of having been made
|
||||
by running the Program). Whether that is true depends on what the Program
|
||||
does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's source
|
||||
code as you receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice and
|
||||
disclaimer of warranty; keep intact all the notices that refer to this License
|
||||
and to the absence of any warranty; and give any other recipients of the
|
||||
Program a copy of this License along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and you
|
||||
may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion of it,
|
||||
thus forming a work based on the Program, and copy and distribute such
|
||||
modifications or work under the terms of Section 1 above, provided that you
|
||||
also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices stating that
|
||||
you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in whole or in
|
||||
part contains or is derived from the Program or any part thereof, to be
|
||||
licensed as a whole at no charge to all third parties under the terms of this
|
||||
License.
|
||||
|
||||
c) If the modified program normally reads commands interactively when run,
|
||||
you must cause it, when started running for such interactive use in the most
|
||||
ordinary way, to print or display an announcement including an appropriate
|
||||
copyright notice and a notice that there is no warranty (or else, saying that
|
||||
you provide a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this License.
|
||||
(Exception: if the Program itself is interactive but does not normally print such
|
||||
an announcement, your work based on the Program is not required to print
|
||||
an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If identifiable
|
||||
sections of that work are not derived from the Program, and can be
|
||||
reasonably considered independent and separate works in themselves, then
|
||||
this License, and its terms, do not apply to those sections when you distribute
|
||||
them as separate works. But when you distribute the same sections as part
|
||||
of a whole which is a work based on the Program, the distribution of the
|
||||
whole must be on the terms of this License, whose permissions for other
|
||||
licensees extend to the entire whole, and thus to each and every part
|
||||
regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest your rights to
|
||||
work written entirely by you; rather, the intent is to exercise the right to
|
||||
control the distribution of derivative or collective works based on the
|
||||
Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of a
|
||||
storage or distribution medium does not bring the other work under the scope
|
||||
of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it, under
|
||||
Section 2) in object code or executable form under the terms of Sections 1
|
||||
and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable source
|
||||
code, which must be distributed under the terms of Sections 1 and 2 above
|
||||
on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three years, to give
|
||||
any third party, for a charge no more than your cost of physically performing
|
||||
source distribution, a complete machine-readable copy of the corresponding
|
||||
source code, to be distributed under the terms of Sections 1 and 2 above on
|
||||
a medium customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer to distribute
|
||||
corresponding source code. (This alternative is allowed only for
|
||||
noncommercial distribution and only if you received the program in object
|
||||
code or executable form with such an offer, in accord with Subsection b
|
||||
above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source code
|
||||
means all the source code for all modules it contains, plus any associated
|
||||
interface definition files, plus the scripts used to control compilation and
|
||||
installation of the executable. However, as a special exception, the source
|
||||
code distributed need not include anything that is normally distributed (in
|
||||
either source or binary form) with the major components (compiler, kernel,
|
||||
and so on) of the operating system on which the executable runs, unless that
|
||||
component itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering access to
|
||||
copy from a designated place, then offering equivalent access to copy the
|
||||
source code from the same place counts as distribution of the source code,
|
||||
even though third parties are not compelled to copy the source along with the
|
||||
object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program except as
|
||||
expressly provided under this License. Any attempt otherwise to copy,
|
||||
modify, sublicense or distribute the Program is void, and will automatically
|
||||
terminate your rights under this License. However, parties who have received
|
||||
copies, or rights, from you under this License will not have their licenses
|
||||
terminated so long as such parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not signed it.
|
||||
However, nothing else grants you permission to modify or distribute the
|
||||
Program or its derivative works. These actions are prohibited by law if you
|
||||
do not accept this License. Therefore, by modifying or distributing the
|
||||
Program (or any work based on the Program), you indicate your acceptance
|
||||
of this License to do so, and all its terms and conditions for copying,
|
||||
distributing or modifying the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the original
|
||||
licensor to copy, distribute or modify the Program subject to these terms and
|
||||
conditions. You may not impose any further restrictions on the recipients'
|
||||
exercise of the rights granted herein. You are not responsible for enforcing
|
||||
compliance by third parties to this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues), conditions
|
||||
are imposed on you (whether by court order, agreement or otherwise) that
|
||||
contradict the conditions of this License, they do not excuse you from the
|
||||
conditions of this License. If you cannot distribute so as to satisfy
|
||||
simultaneously your obligations under this License and any other pertinent
|
||||
obligations, then as a consequence you may not distribute the Program at all.
|
||||
For example, if a patent license would not permit royalty-free redistribution
|
||||
of the Program by all those who receive copies directly or indirectly through
|
||||
you, then the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under any
|
||||
particular circumstance, the balance of the section is intended to apply and
|
||||
the section as a whole is intended to apply in other circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any patents or
|
||||
other property right claims or to contest validity of any such claims; this
|
||||
section has the sole purpose of protecting the integrity of the free software
|
||||
distribution system, which is implemented by public license practices. Many
|
||||
people have made generous contributions to the wide range of software
|
||||
distributed through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing to
|
||||
distribute software through any other system and a licensee cannot impose
|
||||
that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to be a
|
||||
consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in certain
|
||||
countries either by patents or by copyrighted interfaces, the original copyright
|
||||
holder who places the Program under this License may add an explicit
|
||||
geographical distribution limitation excluding those countries, so that
|
||||
distribution is permitted only in or among countries not thus excluded. In such
|
||||
case, this License incorporates the limitation as if written in the body of this
|
||||
License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will be
|
||||
similar in spirit to the present version, but may differ in detail to address new
|
||||
problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program specifies
|
||||
a version number of this License which applies to it and "any later version",
|
||||
you have the option of following the terms and conditions either of that
|
||||
version or of any later version published by the Free Software Foundation. If
|
||||
the Program does not specify a version number of this License, you may
|
||||
choose any version ever published by the Free Software Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free programs
|
||||
whose distribution conditions are different, write to the author to ask for
|
||||
permission. For software which is copyrighted by the Free Software
|
||||
Foundation, write to the Free Software Foundation; we sometimes make
|
||||
exceptions for this. Our decision will be guided by the two goals of
|
||||
preserving the free status of all derivatives of our free software and of
|
||||
promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE,
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT
|
||||
PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE
|
||||
STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
|
||||
OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
|
||||
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
|
||||
PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
|
||||
NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR
|
||||
AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR
|
||||
ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE
|
||||
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL,
|
||||
SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
|
||||
ARISING OUT OF THE USE OR INABILITY TO USE THE
|
||||
PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA
|
||||
OR DATA BEING RENDERED INACCURATE OR LOSSES
|
||||
SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE
|
||||
PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN
|
||||
IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF
|
||||
THE POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
@@ -0,0 +1,12 @@
|
||||
name = simplehtmldom API
|
||||
description = A wrapper module around the simplehtmldom PHP library at sourceforge.
|
||||
package = Filter
|
||||
core = 7.x
|
||||
|
||||
|
||||
; Information added by drupal.org packaging script on 2011-06-13
|
||||
version = "7.x-1.12"
|
||||
core = "7.x"
|
||||
project = "simplehtmldom"
|
||||
datestamp = "1307968619"
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
// Include the only file of the library.
|
||||
// There are 5-6 funcs there and their names are not prefixed. But these names are quite unprobable to cause collisions...
|
||||
|
||||
if (!function_exists('str_get_html')) {
|
||||
require_once('simplehtmldom/simple_html_dom.php');
|
||||
}
|
||||
else {
|
||||
drupal_set_message('The simplehtmldom API module found that you define its functions before it does itself! Somewhere in yor drupal files there must be a copy of the simplehtmldom PHP library that you do not need if you have this drupal module... Please remove the old library and live happily. The simplehtmldom module will include the necessary php files from the lib as soon as you remove the old ones.', 'error');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
// example of how to use advanced selector features
|
||||
include('../simple_html_dom.php');
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// descendant selector
|
||||
$str = <<<HTML
|
||||
<div>
|
||||
<div>
|
||||
<div class="foo bar">ok</div>
|
||||
</div>
|
||||
</div>
|
||||
HTML;
|
||||
|
||||
$html = str_get_html($str);
|
||||
echo $html->find('div div div', 0)->innertext . '<br>'; // result: "ok"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// nested selector
|
||||
$str = <<<HTML
|
||||
<ul id="ul1">
|
||||
<li>item:<span>1</span></li>
|
||||
<li>item:<span>2</span></li>
|
||||
</ul>
|
||||
<ul id="ul2">
|
||||
<li>item:<span>3</span></li>
|
||||
<li>item:<span>4</span></li>
|
||||
</ul>
|
||||
HTML;
|
||||
|
||||
$html = str_get_html($str);
|
||||
foreach($html->find('ul') as $ul) {
|
||||
foreach($ul->find('li') as $li)
|
||||
echo $li->innertext . '<br>';
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// parsing checkbox
|
||||
$str = <<<HTML
|
||||
<form name="form1" method="post" action="">
|
||||
<input type="checkbox" name="checkbox1" value="checkbox1" checked>item1<br>
|
||||
<input type="checkbox" name="checkbox2" value="checkbox2">item2<br>
|
||||
<input type="checkbox" name="checkbox3" value="checkbox3" checked>item3<br>
|
||||
</form>
|
||||
HTML;
|
||||
|
||||
$html = str_get_html($str);
|
||||
foreach($html->find('input[type=checkbox]') as $checkbox) {
|
||||
if ($checkbox->checked)
|
||||
echo $checkbox->name . ' is checked<br>';
|
||||
else
|
||||
echo $checkbox->name . ' is not checked<br>';
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
// example of how to use basic selector to retrieve HTML contents
|
||||
include('../simple_html_dom.php');
|
||||
|
||||
// get DOM from URL or file
|
||||
$html = file_get_html('http://www.google.com/');
|
||||
|
||||
// find all link
|
||||
foreach($html->find('a') as $e)
|
||||
echo $e->href . '<br>';
|
||||
|
||||
// find all image
|
||||
foreach($html->find('img') as $e)
|
||||
echo $e->src . '<br>';
|
||||
|
||||
// find all image with full tag
|
||||
foreach($html->find('img') as $e)
|
||||
echo $e->outertext . '<br>';
|
||||
|
||||
// find all div tags with id=gbar
|
||||
foreach($html->find('div#gbar') as $e)
|
||||
echo $e->innertext . '<br>';
|
||||
|
||||
// find all span tags with class=gb1
|
||||
foreach($html->find('span.gb1') as $e)
|
||||
echo $e->outertext . '<br>';
|
||||
|
||||
// find all td tags with attribite align=center
|
||||
foreach($html->find('td[align=center]') as $e)
|
||||
echo $e->innertext . '<br>';
|
||||
|
||||
// extract text from table
|
||||
echo $html->find('td[align="center"]', 1)->plaintext.'<br><hr>';
|
||||
|
||||
// extract text from HTML
|
||||
echo $html->plaintext;
|
||||
?>
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
include_once('../simple_html_dom.php');
|
||||
|
||||
|
||||
// 1. Write a function with parameter "$element"
|
||||
function my_callback($element) {
|
||||
if ($element->tag=='input')
|
||||
$element->outertext = 'input';
|
||||
|
||||
if ($element->tag=='img')
|
||||
$element->outertext = 'img';
|
||||
|
||||
if ($element->tag=='a')
|
||||
$element->outertext = 'a';
|
||||
}
|
||||
|
||||
|
||||
// 2. create HTML Dom
|
||||
$html = file_get_html('http://www.google.com/');
|
||||
|
||||
|
||||
// 3. Register the callback function with it's function name
|
||||
$html->set_callback('my_callback');
|
||||
|
||||
|
||||
// 4. Callback function will be invoked while dumping
|
||||
echo $html;
|
||||
?>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
include_once('../simple_html_dom.php');
|
||||
|
||||
echo file_get_html('http://www.google.com/')->plaintext;
|
||||
?>
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
// example of how to modify HTML contents
|
||||
include('../simple_html_dom.php');
|
||||
|
||||
// get DOM from URL or file
|
||||
$html = file_get_html('http://www.google.com/');
|
||||
|
||||
// remove all image
|
||||
foreach($html->find('img') as $e)
|
||||
$e->outertext = '';
|
||||
|
||||
// replace all input
|
||||
foreach($html->find('input') as $e)
|
||||
$e->outertext = '[INPUT]';
|
||||
|
||||
// dump contents
|
||||
echo $html;
|
||||
?>
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
include_once('../../simple_html_dom.php');
|
||||
|
||||
function scraping_digg() {
|
||||
// create HTML DOM
|
||||
$html = file_get_html('http://digg.com/');
|
||||
|
||||
// get news block
|
||||
foreach($html->find('div.news-summary') as $article) {
|
||||
// get title
|
||||
$item['title'] = trim($article->find('h3', 0)->plaintext);
|
||||
// get details
|
||||
$item['details'] = trim($article->find('p', 0)->plaintext);
|
||||
// get intro
|
||||
$item['diggs'] = trim($article->find('li a strong', 0)->plaintext);
|
||||
|
||||
$ret[] = $item;
|
||||
}
|
||||
|
||||
// clean up memory
|
||||
$html->clear();
|
||||
unset($html);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// test it!
|
||||
|
||||
// "http://digg.com" will check user_agent header...
|
||||
ini_set('user_agent', 'My-Application/2.5');
|
||||
|
||||
$ret = scraping_digg();
|
||||
|
||||
foreach($ret as $v) {
|
||||
echo $v['title'].'<br>';
|
||||
echo '<ul>';
|
||||
echo '<li>'.$v['details'].'</li>';
|
||||
echo '<li>Diggs: '.$v['diggs'].'</li>';
|
||||
echo '</ul>';
|
||||
}
|
||||
|
||||
?>
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
include_once('../../simple_html_dom.php');
|
||||
|
||||
function scraping_IMDB($url) {
|
||||
// create HTML DOM
|
||||
$html = file_get_html($url);
|
||||
|
||||
// get title
|
||||
$ret['Title'] = $html->find('title', 0)->innertext;
|
||||
|
||||
// get rating
|
||||
$ret['Rating'] = $html->find('div[class="general rating"] b', 0)->innertext;
|
||||
|
||||
// get overview
|
||||
foreach($html->find('div[class="info"]') as $div) {
|
||||
// skip user comments
|
||||
if($div->find('h5', 0)->innertext=='User Comments:')
|
||||
return $ret;
|
||||
|
||||
$key = '';
|
||||
$val = '';
|
||||
|
||||
foreach($div->find('*') as $node) {
|
||||
if ($node->tag=='h5')
|
||||
$key = $node->plaintext;
|
||||
|
||||
if ($node->tag=='a' && $node->plaintext!='more')
|
||||
$val .= trim(str_replace("\n", '', $node->plaintext));
|
||||
|
||||
if ($node->tag=='text')
|
||||
$val .= trim(str_replace("\n", '', $node->plaintext));
|
||||
}
|
||||
|
||||
$ret[$key] = $val;
|
||||
}
|
||||
|
||||
// clean up memory
|
||||
$html->clear();
|
||||
unset($html);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// test it!
|
||||
$ret = scraping_IMDB('http://imdb.com/title/tt0335266/');
|
||||
|
||||
foreach($ret as $k=>$v)
|
||||
echo '<strong>'.$k.' </strong>'.$v.'<br>';
|
||||
?>
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
include_once('../../simple_html_dom.php');
|
||||
|
||||
function scraping_slashdot() {
|
||||
// create HTML DOM
|
||||
$html = file_get_html('http://slashdot.org/');
|
||||
|
||||
// get article block
|
||||
foreach($html->find('div[id^=firehose-]') as $article) {
|
||||
// get title
|
||||
$item['title'] = trim($article->find('a.datitle', 0)->plaintext);
|
||||
// get body
|
||||
$item['body'] = trim($article->find('div.body', 0)->plaintext);
|
||||
|
||||
$ret[] = $item;
|
||||
}
|
||||
|
||||
// clean up memory
|
||||
$html->clear();
|
||||
unset($html);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// test it!
|
||||
$ret = scraping_slashdot();
|
||||
|
||||
foreach($ret as $v) {
|
||||
echo $v['title'].'<br>';
|
||||
echo '<ul>';
|
||||
echo '<li>'.$v['body'].'</li>';
|
||||
echo '</ul>';
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
include_once('../simple_html_dom.php');
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// remove HTML comments
|
||||
function html_no_comment($url) {
|
||||
// create HTML DOM
|
||||
$html = file_get_html($url);
|
||||
|
||||
// remove all comment elements
|
||||
foreach($html->find('comment') as $e)
|
||||
$e->outertext = '';
|
||||
|
||||
$ret = $html->save();
|
||||
|
||||
// clean up memory
|
||||
$html->clear();
|
||||
unset($html);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// search elements that contains an specific text
|
||||
function find_contains($html, $selector, $keyword, $index=-1) {
|
||||
$ret = array();
|
||||
foreach ($html->find($selector) as $e) {
|
||||
if (strpos($e->innertext, $keyword)!==false)
|
||||
$ret[] = $e;
|
||||
}
|
||||
|
||||
if ($index<0) return $ret;
|
||||
return (isset($ret[$index])) ? $ret[$index] : null;
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,113 @@
|
||||
/*$Rev: 46 $*/
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font-family: verdana,arial,helvetica,sans-serif;
|
||||
font-size: 11px;
|
||||
color: #4F5155;
|
||||
}
|
||||
|
||||
#content {
|
||||
margin: 0 20px 0 20px;
|
||||
line-height: 16px;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 18px;
|
||||
margin: 0;
|
||||
padding: 0 0 2px 0;
|
||||
background-color: #D0D0D0;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
h2 {
|
||||
background-color: #727EA3;
|
||||
border-right: 1px solid #D0D0D0;
|
||||
border-bottom: 1px solid #D0D0D0;
|
||||
color: #FFFFFF;
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
margin: 14px 0 4px 0;
|
||||
padding: 1px 10px 1px 10px;
|
||||
}
|
||||
|
||||
ul {
|
||||
margin-top: 0;
|
||||
margin-bottom: 0;
|
||||
line-height:1.5em;
|
||||
list-style-image:url(bullet.gif);
|
||||
list-style-type:square;
|
||||
}
|
||||
|
||||
.top {
|
||||
font-size: 11px;
|
||||
float: right;
|
||||
}
|
||||
|
||||
.code {
|
||||
font-size: 11px;
|
||||
font-family: Monaco, Verdana, Sans-serif;
|
||||
line-height: 14px;
|
||||
background-color: #f6f6f6;
|
||||
border-bottom: 1px solid #D0D0D0;
|
||||
border-top: 1px solid #A0A0A0;
|
||||
border-left: 1px solid #A0A0A0;
|
||||
border-right: 1px solid #D0D0D0;
|
||||
color: #002166;
|
||||
display: block;
|
||||
margin: 2px 0 2px 0;
|
||||
padding: 2px 10px 2px 10px;
|
||||
}
|
||||
|
||||
.code A:link {color: #002166; text-decoration: none; font-weight: bold;}
|
||||
.code A:visited {color: #002166; text-decoration: none; font-weight: bold;}
|
||||
.code A:active {color: #002166; text-decoration: none; font-weight: bold;}
|
||||
.code A:hover {color: #0000ff; text-decoration: underline; font-weight: bold;}
|
||||
|
||||
.code .keyword {
|
||||
color: #007700;
|
||||
}
|
||||
|
||||
.code .comment {
|
||||
font-size: 10px;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.code .var {
|
||||
color: #770000;
|
||||
}
|
||||
|
||||
th {
|
||||
font-family: Lucida Grande, Verdana, Geneva, Sans-serif;
|
||||
color: #000000;
|
||||
background-color: #CFD4E6;
|
||||
margin: 2px 2px 2px 2px;
|
||||
padding: 2px 2px 2px 2px;
|
||||
font-size: 13px;
|
||||
font-weight: normal;
|
||||
font-style: normal;
|
||||
}
|
||||
|
||||
td {
|
||||
background-color: #dddddd;
|
||||
}
|
||||
|
||||
.description {
|
||||
font-family: Lucida Grande, Verdana, Geneva, Sans-serif;
|
||||
font-size: 11px;
|
||||
color: #333;
|
||||
text-ident: 30px;
|
||||
font-style: normal;
|
||||
}
|
||||
|
||||
.returns {
|
||||
font-family: Monaco, Verdana, Sans-serif;
|
||||
font-size: 10px;
|
||||
color: #888;
|
||||
float: left;
|
||||
text-align: right;
|
||||
margin: 0 4px 0 0;
|
||||
width: 48px;
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/* Caution! Ensure accessibility in print and other media types... */
|
||||
@media projection, screen { /* Use class for showing/hiding tab content, so that visibility can be better controlled in different media types... */
|
||||
.ui-tabs-hide {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
/* Hide useless elements in print layouts... */
|
||||
@media print {
|
||||
.ui-tabs-nav {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
/* Skin */
|
||||
.ui-tabs-nav, .ui-tabs-panel {
|
||||
font-family: "Trebuchet MS", Trebuchet, Verdana, Helvetica, Arial, sans-serif;
|
||||
font-size: 12px;
|
||||
}
|
||||
.ui-tabs-nav {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0 0 0 4px;
|
||||
}
|
||||
.ui-tabs-nav:after { /* clearing without presentational markup, IE gets extra treatment */
|
||||
display: block;
|
||||
clear: both;
|
||||
content: " ";
|
||||
}
|
||||
.ui-tabs-nav li {
|
||||
float: left;
|
||||
margin: 0 0 0 1px;
|
||||
min-width: 84px; /* be nice to Opera */
|
||||
}
|
||||
.ui-tabs-nav a, .ui-tabs-nav a span {
|
||||
display: block;
|
||||
padding: 0 10px;
|
||||
background: url(../img/tab.png) no-repeat;
|
||||
}
|
||||
.ui-tabs-nav a {
|
||||
margin: 1px 0 0; /* position: relative makes opacity fail for disabled tab in IE */
|
||||
padding-left: 0;
|
||||
color: #27537a;
|
||||
font-weight: bold;
|
||||
line-height: 1.2;
|
||||
text-align: center;
|
||||
text-decoration: none;
|
||||
white-space: nowrap; /* required in IE 6 */
|
||||
outline: 0; /* prevent dotted border in Firefox */
|
||||
}
|
||||
.ui-tabs-nav .ui-tabs-selected a {
|
||||
position: relative;
|
||||
top: 1px;
|
||||
z-index: 2;
|
||||
margin-top: 0;
|
||||
color: #000;
|
||||
}
|
||||
.ui-tabs-nav a span {
|
||||
width: 64px; /* IE 6 treats width as min-width */
|
||||
min-width: 64px;
|
||||
height: 18px; /* IE 6 treats height as min-height */
|
||||
min-height: 18px;
|
||||
padding-top: 6px;
|
||||
padding-right: 0;
|
||||
}
|
||||
*>.ui-tabs-nav a span { /* hide from IE 6 */
|
||||
width: auto;
|
||||
height: auto;
|
||||
}
|
||||
.ui-tabs-nav .ui-tabs-selected a span {
|
||||
padding-bottom: 1px;
|
||||
}
|
||||
.ui-tabs-nav .ui-tabs-selected a, .ui-tabs-nav a:hover, .ui-tabs-nav a:focus, .ui-tabs-nav a:active {
|
||||
background-position: 100% -150px;
|
||||
}
|
||||
.ui-tabs-nav a, .ui-tabs-nav .ui-tabs-disabled a:hover, .ui-tabs-nav .ui-tabs-disabled a:focus, .ui-tabs-nav .ui-tabs-disabled a:active {
|
||||
background-position: 100% -100px;
|
||||
}
|
||||
.ui-tabs-nav .ui-tabs-selected a span, .ui-tabs-nav a:hover span, .ui-tabs-nav a:focus span, .ui-tabs-nav a:active span {
|
||||
background-position: 0 -50px;
|
||||
}
|
||||
.ui-tabs-nav a span, .ui-tabs-nav .ui-tabs-disabled a:hover span, .ui-tabs-nav .ui-tabs-disabled a:focus span, .ui-tabs-nav .ui-tabs-disabled a:active span {
|
||||
background-position: 0 0;
|
||||
}
|
||||
.ui-tabs-nav .ui-tabs-selected a:link, .ui-tabs-nav .ui-tabs-selected a:visited, .ui-tabs-nav .ui-tabs-disabled a:link, .ui-tabs-nav .ui-tabs-disabled a:visited { /* @ Opera, use pseudo classes otherwise it confuses cursor... */
|
||||
cursor: text;
|
||||
}
|
||||
.ui-tabs-nav a:hover, .ui-tabs-nav a:focus, .ui-tabs-nav a:active,
|
||||
.ui-tabs-nav .ui-tabs-unselect a:hover, .ui-tabs-nav .ui-tabs-unselect a:focus, .ui-tabs-nav .ui-tabs-unselect a:active { /* @ Opera, we need to be explicit again here now... */
|
||||
cursor: pointer;
|
||||
}
|
||||
.ui-tabs-disabled {
|
||||
opacity: .4;
|
||||
filter: alpha(opacity=40);
|
||||
}
|
||||
.ui-tabs-panel {
|
||||
border-top: 1px solid #97a5b0;
|
||||
padding: 2px 4px 2px 4px;
|
||||
background: #fff; /* declare background color for container to avoid distorted fonts in IE while fading */
|
||||
border: 1px solid #D0D0D0;
|
||||
border-bottom: 1px solid #A0A0A0;
|
||||
border-right: 1px solid #A0A0A0;
|
||||
}
|
||||
.ui-tabs-loading em {
|
||||
padding: 0 0 0 20px;
|
||||
background: url(loading.gif) no-repeat 0 50%;
|
||||
}
|
||||
|
||||
/* Additional IE specific bug fixes... */
|
||||
* html .ui-tabs-nav { /* auto clear, @ IE 6 & IE 7 Quirks Mode */
|
||||
display: inline-block;
|
||||
}
|
||||
*:first-child+html .ui-tabs-nav { /* @ IE 7 Standards Mode - do not group selectors, otherwise IE 6 will ignore complete rule (because of the unknown + combinator)... */
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 734 B |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,448 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
<title>PHP Simple HTML DOM Parser: Manual</title>
|
||||
<link href="css/default.css" rel="stylesheet" type="text/css">
|
||||
<link rel="stylesheet" href="css/ui.tabs.css" type="text/css" media="print, projection, screen">
|
||||
<script type="text/javascript" src="js/jquery-1.2.3.pack.js"></script>
|
||||
<script type="text/javascript" src="js/ui.tabs.pack.js"></script>
|
||||
<script language="JavaScript" type="text/JavaScript">
|
||||
$(document).ready(function(){
|
||||
$(function() {$('#container_quickstart > ul').tabs();});
|
||||
$(function() {$('#container_create > ul').tabs();});
|
||||
$(function() {$('#container_find > ul').tabs();});
|
||||
$(function() {$('#container_access > ul').tabs();});
|
||||
$(function() {$('#container_traverse > ul').tabs();});
|
||||
$(function() {$('#container_dump > ul').tabs();});
|
||||
$(function() {$('#container_callback > ul').tabs();});
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h1><a name="top"></a>PHP Simple HTML DOM Parser Manual</h1>
|
||||
<div id="content">
|
||||
<h2>Index</h2>
|
||||
<ul>
|
||||
<li><a href="#section_quickstart">Quick Start</a></li>
|
||||
<li><a href="#section_create">How to create HTML DOM object?</a></li>
|
||||
<li><a href="#section_find">How to find HTML elements?</a></li>
|
||||
<li><a href="#section_access">How to access the HTML element's attributes?</a> </li>
|
||||
<li><a href="#section_traverse">How to traverse the DOM tree?</a></li>
|
||||
<li><a href="#section_dump">How to dump contents of DOM object?</a></li>
|
||||
<li><a href="#section_callback">How to customize the parsing behavior?</a></li>
|
||||
<li><a href="manual_api.htm">API Reference</a></li>
|
||||
<li><a href="manual_faq.htm">FAQ</a></li>
|
||||
</ul>
|
||||
|
||||
<a name="section_quickstart"></a>
|
||||
<h2>Quick Start</h2>
|
||||
<a class="top" href="#top">Top</a>
|
||||
<div id="container_quickstart">
|
||||
<ul>
|
||||
<li><a href="#fragment-11"><span>Get HTML elements</span></a></li>
|
||||
<li><a href="#fragment-12"><span>Modify HTML elements</span></a></li>
|
||||
<li><a href="#fragment-13"><span>Extract contents from HTML</span></a></li>
|
||||
<li><a href="#fragment-14"><span>Scraping Slashdot!</span></a></li>
|
||||
</ul>
|
||||
<div id="fragment-11">
|
||||
<div class="code">
|
||||
<span class="comment">// Create DOM from URL or file</span><br>
|
||||
$html = <strong>file_get_html</strong>(<span class="var">'http://www.google.com/'</span>);<br>
|
||||
<br>
|
||||
<span class="comment">// Find all images </span><br>
|
||||
foreach($html-><strong>find</strong>(<span class="var">'img'</span>) as $element) <br>
|
||||
echo $element-><strong>src</strong> . <span class="var">'<br>'</span>;<br>
|
||||
<br>
|
||||
<span class="comment">// Find all links </span><br>
|
||||
foreach($html-><strong>find</strong>(<span class="var">'a'</span>) as $element) <br>
|
||||
echo $element-><strong>href</strong> . <span class="var">'<br>'</span>; </div>
|
||||
</div>
|
||||
<div id="fragment-12">
|
||||
<div class="code">
|
||||
<span class="comment">// Create DOM from string</span><br>
|
||||
$html = <strong>str_get_html</strong>(<span class="var">'<div id="hello">Hello</div><div id="world">World</div>'</span>);<span class="comment"><br>
|
||||
<br>
|
||||
</span>
|
||||
|
||||
$html-><strong>find</strong>(<span class="var">'div', 1</span>)-><strong>class</strong> = <span class="var">'bar'</span>;<br>
|
||||
<br>
|
||||
$html-><strong>find</strong>(<span class="var">'div[id=hello]', 0</span>)-><strong>innertext</strong> = <span class="var">'foo'</span>;<br>
|
||||
<br>
|
||||
echo $html; <span class="comment">// Output: <div id="hello"><strong>foo</strong></div><div id="world" <strong>class="bar"</strong>>World</div></span> </div>
|
||||
</div>
|
||||
<div id="fragment-13">
|
||||
<div class="code"><br>
|
||||
<span class="comment">// Dump contents (without tags) from HTML</span><br>
|
||||
echo <strong>file_get_html</strong>(<span class="var">'http://www.google.com/'</span>)-><strong>plaintext</strong>;
|
||||
<br>
|
||||
<br>
|
||||
</div>
|
||||
</div>
|
||||
<div id="fragment-14">
|
||||
<div class="code">
|
||||
<span class="comment">// Create DOM from URL</span><br>
|
||||
$html = <strong>file_get_html</strong>(<span class="var">'http://slashdot.org/'</span>);<br>
|
||||
<br>
|
||||
<span class="comment">// Find all article blocks</span><br>
|
||||
foreach($html-><strong>find</strong>(<span class="var">'div.article'</span>) as $article) {<br>
|
||||
$item[<span class="var">'title'</span>] = $article-><strong>find</strong>(<span class="var">'div.title'</span>, <span class="var">0</span>)-><strong>plaintext</strong>;<br>
|
||||
$item[<span class="var">'intro'</span>] = $article-><strong>find</strong>(<span class="var">'div.intro'</span>, <span class="var">0</span>)-><strong>plaintext</strong>;<br>
|
||||
$item[<span class="var">'details'</span>] = $article-><strong>find</strong>(<span class="var">'div.details'</span>, <span class="var">0</span>)-><strong>plaintext</strong>;<br>
|
||||
$articles[] = $item;<br>
|
||||
}<br>
|
||||
<br>
|
||||
print_r($articles);
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<a name="section_create"></a>
|
||||
<h2>How to create HTML DOM object?</h2>
|
||||
<a class="top" href="#top">Top</a>
|
||||
<div id="container_create">
|
||||
<ul>
|
||||
<li><a href="#frag_create_quick"><span>Quick way</span></a></li>
|
||||
<li><a href="#frag_create_oo"><span>Object-oriented way</span></a></li>
|
||||
</ul>
|
||||
<div id="frag_create_quick">
|
||||
<div class="code"><span class="comment">// Create a DOM object from a string</span><br>
|
||||
$html = <strong>str_get_html</strong>(<span class="var">'<html><body>Hello!</body></html>'</span>);<br>
|
||||
<br>
|
||||
<span class="comment">// Create a DOM object from a URL</span><br>
|
||||
$html = <strong>file_get_html</strong>(<span class="var">'http://www.google.com/'</span>);<br>
|
||||
<br>
|
||||
<span class="comment">// Create a DOM object from a HTML file</span><br>
|
||||
$html = <strong>file_get_html</strong>(<span class="var">'test.htm'</span>);<span class="comment"><br>
|
||||
</span></div>
|
||||
</div>
|
||||
<div id="frag_create_oo">
|
||||
<div class="code"><span class="comment">// Create a DOM object</span><br>
|
||||
$html = new <strong>simple_html_dom</strong>();<br>
|
||||
<br>
|
||||
<span class="comment">// Load HTML from a string</span><br>
|
||||
$html-><strong>load</strong>(<span class="var">'<html><body>Hello!</body></html>'</span>);<br>
|
||||
<br>
|
||||
<span class="comment">// Load HTML from a URL </span> <br>
|
||||
$html-><strong>load_file</strong>(<span class="var">'http://www.google.com/'</span>);<br>
|
||||
<br>
|
||||
<span class="comment">// Load HTML from a HTML file</span> <br>
|
||||
$html-><strong>load_file</strong>(<span class="var">'test.htm'</span>);</div>
|
||||
</div>
|
||||
</div>
|
||||
<a name="section_find"></a>
|
||||
<h2>How to find HTML elements?</h2>
|
||||
<a class="top" href="#top">Top</a>
|
||||
<div id="container_find">
|
||||
<ul>
|
||||
<li><a href="#frag_find_basic"><span>Basics</span></a></li>
|
||||
<li><a href="#frag_find_advanced"><span>Advanced</span></a></li>
|
||||
<li><a href="#frag_find_chain"><span>Descendant selectors</span></a></li>
|
||||
<li><a href="#frag_find_nested"><span>Nested selectors</span></a></li>
|
||||
<li><a href="#frag_find_attr"><span>Attribute Filters</span></a></li>
|
||||
<li><a href="#frag_find_textcomment"><span>Text & Comments</span></a></li>
|
||||
</ul>
|
||||
<div id="frag_find_basic">
|
||||
<div class="code"> <span class="comment">// Find all <strong>anchors</strong>, returns a <strong>array</strong> of element objects</span><br>
|
||||
$ret = $html->find(<span class="var">'<strong>a</strong>'</span>);<br>
|
||||
<br>
|
||||
<span class="comment">// Find <strong>(N)th</strong> <strong>anchor</strong>, returns element object or <strong>null</strong> if not found</span> <span class="comment">(zero based)</span><br>
|
||||
$ret = $html->find(<span class="var">'<strong>a</strong>', <strong>0</strong></span>);<br>
|
||||
<br>
|
||||
<span class="comment">// Find <strong>lastest</strong> <strong>anchor</strong>, returns element object or <strong>null</strong> if not found</span> <span class="comment">(zero based)</span><br>
|
||||
$ret = $html->find(<span class="var">'<strong>a</strong>', <strong>-1</strong></span>); <br>
|
||||
<br>
|
||||
<span class="comment">// Find all <strong><div></strong> with the <strong>id</strong> attribute</span><br>
|
||||
$ret = $html->find(<span class="var">'<strong>div[id]</strong>'</span>);<br>
|
||||
<br>
|
||||
<span class="comment">// Find all <strong><div></strong> which attribute <strong>id=foo</strong></span><br>
|
||||
$ret = $html->find(<span class="var">'<strong>div[id=foo]</strong>'</span>); <br>
|
||||
</div>
|
||||
</div>
|
||||
<div id="frag_find_advanced">
|
||||
<div class="code"><span class="comment">// Find all element which <strong>id</strong>=foo</span><br>
|
||||
$ret = $html->find(<span class="var">'<strong>#foo</strong>'</span>);<br>
|
||||
<br>
|
||||
<span class=comment>// Find all element which <strong>class</strong>=foo</span><br>
|
||||
$ret = $html->find(<span class=var>'<strong>.foo</strong>'</span>);<br>
|
||||
<br>
|
||||
<span class="comment">// Find all element has attribute<strong> id</strong></span><br>
|
||||
$ret = $html->find(<span class="var">'<strong>*[id]</strong>'</span>); <br>
|
||||
<br>
|
||||
<span class="comment">// Find all <strong>anchors</strong> and <strong>images</strong> </span><br>
|
||||
$ret = $html->find(<span class="var">'<strong>a, img</strong>'</span>); <br>
|
||||
<br>
|
||||
<span class="comment">// Find all <strong>anchors</strong> and <strong>images</strong> with the "title" attribute</span><br>
|
||||
$ret = $html->find(<span class="var">'<strong>a[title], img[title]</strong>'</span>);<br>
|
||||
</div>
|
||||
</div>
|
||||
<div id="frag_find_attr">
|
||||
<div class="code">
|
||||
Supports these operators in attribute selectors:<br><br>
|
||||
<table cellpadding="1" cellspacing="1">
|
||||
<tr>
|
||||
<th width="25%">Filter</th>
|
||||
<th width="75%">Description</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>[attribute]</td>
|
||||
<td>Matches elements that <strong>have</strong> the specified attribute.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>[!attribute]</td>
|
||||
<td>Matches elements that <strong>don't have</strong> the specified attribute.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>[attribute=value]</td>
|
||||
<td>Matches elements that have the specified attribute with a <strong>certain value</strong>.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>[attribute!=value]</td>
|
||||
<td>Matches elements that <strong>don't have</strong> the specified attribute with a certain value.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>[attribute^=value]</td>
|
||||
<td>Matches elements that have the specified attribute and it <strong>starts</strong> with a certain value.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>[attribute$=value]</td>
|
||||
<td>Matches elements that have the specified attribute and it <strong>ends</strong> with a certain value.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>[attribute*=value]</td>
|
||||
<td>Matches elements that have the specified attribute and it <strong>contains</strong> a certain value.</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<div id="frag_find_chain">
|
||||
<div class="code"><span class="comment">// Find all <li> in <ul> </span><br>
|
||||
$es = $html->find(<span class="var">'<strong>ul li</strong>'</span>);<br>
|
||||
<br>
|
||||
<span class="comment">// Find Nested <div> </span><span class="comment">tags</span><br>
|
||||
$es = $html->find(<span class="var">'<strong>div div div</strong>'</span>); <br>
|
||||
<br>
|
||||
<span class="comment">// Find all <td> in <table> which class=hello </span><br>
|
||||
$es = $html->find(<span class="var">'<strong>table.hello td</strong>'</span>);<br>
|
||||
<br>
|
||||
<span class="comment">// Find all td tags with attribite align=center in table tags </span><br>
|
||||
$es = $html->find(<span class="var">''<strong>table</strong><strong> td[align=center]</strong>'</span>);<br>
|
||||
</div>
|
||||
</div>
|
||||
<div id="frag_find_textcomment">
|
||||
<div class="code"><span class="comment"> // Find all text blocks </span><br>
|
||||
$es = $html->find(<span class="var">'<strong>text</strong>'</span>);<br>
|
||||
<br>
|
||||
<span class="comment">// Find all comment (<!--...-->) blocks </span><br>
|
||||
$es = $html->find(<span class="var">'<strong>comment</strong>'</span>);<br>
|
||||
</div>
|
||||
</div>
|
||||
<div id="frag_find_nested">
|
||||
<div class="code"> <span class="comment">// Find all <li> in <ul> </span><br>
|
||||
foreach($html->find(<span class="var">'<strong>ul</strong>'</span>) as $ul) <br>
|
||||
{<br>
|
||||
foreach($ul->find(<span class="var">'<strong>li</strong>'</span>) as $li) <br>
|
||||
{<br>
|
||||
<span class="comment">// do something...</span><br>
|
||||
}<br>
|
||||
}<br>
|
||||
<br>
|
||||
<span class="comment">// Find first <li> in first <ul></span> <br>
|
||||
$e = $html->find(<span class="var">'<strong>ul</strong>', <strong>0</strong></span>)->find(<span class="var">'<strong>li</strong>', <strong>0</strong></span>);<br>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<a name="section_access"></a>
|
||||
<h2>How to access the HTML element's attributes?</h2>
|
||||
<a class="top" href="#top">Top</a>
|
||||
<div id="container_access">
|
||||
<ul>
|
||||
<li><a href="#frag_access_attr"><span>Get, Set and Remove attributes</span></a></li>
|
||||
<li><a href="#frag_access_special"><span>Magic attributes</span></a></li>
|
||||
<li><a href="#frag_access_tips"><span>Tips</span></a></li>
|
||||
</ul>
|
||||
<div id="frag_access_attr">
|
||||
<div class="code">
|
||||
<span class="comment">// <strong>Get</strong> a attribute ( If the attribute is <strong>non-value</strong> attribute (eg. checked, selected...), it will returns <strong>true</strong> or <strong>false</strong>)</span><br>
|
||||
$value = $e-><strong>href</strong>;<br>
|
||||
<br>
|
||||
<span class="comment">// <strong>Set</strong> a attribute(If the attribute is <strong>non-value</strong> attribute (eg. checked, selected...), set it's value as <strong>true</strong> or <strong>false</strong>)</span><br>
|
||||
$e-><strong>href</strong> = <span class="var">'my link'</span>;<br>
|
||||
<br>
|
||||
<span class="comment">// <strong>Remove</strong> a attribute, set it's value as null! </span><br>
|
||||
$e-><strong>href</strong> = <strong><span class="var">null</span></strong>;<br>
|
||||
<br>
|
||||
<span class="comment">// <strong>Determine</strong> whether a attribute exist?</span> <br>
|
||||
if(isset($e-><strong>href</strong>)) <br>
|
||||
echo <span class="var">'href exist!'</span>;<br>
|
||||
</div>
|
||||
</div>
|
||||
<div id="frag_access_special">
|
||||
<div class="code"> <span class="comment">// Example</span><br>
|
||||
<span class="hl-var">$</span><span class="hl-code">html = </span>str_get_html<span class="hl-brackets">(</span><span class="var">"<div>foo <b>bar</b></div>"</span><span class="hl-brackets">)</span><span class="hl-code">;</span> <br>
|
||||
$e = $html->find(<span class="var">"div"</span>, <span class="var">0</span>);<br>
|
||||
<br>
|
||||
echo $e-><strong>tag</strong>; <span class="comment">// Returns: " <strong>div</strong>"</span><br>
|
||||
echo $e-><strong>outertext</strong>; <span class="comment">// Returns: " <strong><div>foo <b>bar</b></div></strong>"</span><br>
|
||||
echo $e-><strong>innertext</strong>; <span class="comment">// Returns: " <strong>foo <b>bar</b></strong>"</span><br>
|
||||
echo $e-><strong>plaintext</strong>; <span class="comment">// Returns: " <strong>foo </strong><strong>bar</strong>"<br>
|
||||
<br>
|
||||
</span>
|
||||
<table cellspacing="1" cellpadding="1">
|
||||
<tr bgcolor="#CCCCCC">
|
||||
<th width="25%">Attribute Name</th>
|
||||
<th width="75%">Usage</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>$e-><strong>tag</strong></td>
|
||||
<td>Read or write the <strong>tag name</strong> of element.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>$e-><strong>outertext</strong></td>
|
||||
<td>Read or write the <strong>outer HTML text </strong> of element.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>$e-><strong>innertext</strong></td>
|
||||
<td>Read or write the <strong>inner HTML text </strong> of element.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>$e-><strong>plaintext</strong></td>
|
||||
<td>Read or write the <strong>plain text </strong> of element.</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<div id="frag_access_tips">
|
||||
<div class="code"><span class="comment">// <strong>Extract</strong> contents from HTML </span><br>
|
||||
echo <strong>$html</strong>-><strong>plaintext</strong>;<br>
|
||||
<br>
|
||||
<span class="comment">
|
||||
// <strong>Wrap</strong> a element</span><br>
|
||||
$e-><strong>outertext</strong> = <span class="var">'<div class="wrap">'</span> . $e-><strong>outertext</strong> . <span class="var">'<div></span>';<br>
|
||||
<br>
|
||||
<span class="comment">// <strong>Remove</strong> a element, set it's outertext as an empty string </span><br>
|
||||
$e-><strong>outertext</strong> = <span class="var">''</span>;<br>
|
||||
<br>
|
||||
<span class="comment">// <strong>Append</strong> a element</span><br>
|
||||
$e-><strong>outertext</strong> = $e-><strong>outertext</strong> . <span class="var">'<div>foo</span><span class="var"><div></span>';<br>
|
||||
<br>
|
||||
<span class="comment">// <strong>Insert</strong> a element</span><br>
|
||||
$e-><strong>outertext</strong> = <span class="var">'<div>foo</span><span class="var"><div></span>' . $e-><strong>outertext</strong>;<br>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<a name="section_traverse"></a>
|
||||
<h2>How to traverse the DOM tree?</h2>
|
||||
<a class="top" href="#top">Top</a>
|
||||
<div id="container_traverse">
|
||||
<ul>
|
||||
<li><a href="#frag_traverse_background"><span>Background Knowledge</span></a></li>
|
||||
<li><a href="#frag_traverse_traverse"><span>Traverse the DOM tree</span></a></li>
|
||||
</ul>
|
||||
<div id="frag_traverse_background">
|
||||
<div class="code"> <span class="comment">// If you are not so familiar with HTML DOM, check this <a href="http://php.net/manual/en/book.dom.php" target="_blank"><span class="var">link</span></a> to learn more... </span><br>
|
||||
<br>
|
||||
<span class="comment">// Example</span><br>
|
||||
echo $html-><strong>find</strong>(<span class="var">"#div1", 0</span>)-><strong>children</strong>(<span class="var">1</span>)-><strong>children</strong>(<span class="var">1</span>)-><strong>children</strong>(<span class="var">2</span>)-><span class="var">id</span>;<br>
|
||||
<span class="comment">// or</span> <br>
|
||||
echo $html-><strong>getElementById</strong>(<span class="var">"div1"</span>)-><strong>childNodes</strong>(<span class="var">1</span>)-><strong>childNodes</strong>(<span class="var">1</span>)-><strong>childNodes</strong>(<span class="var">2</span>)-><strong>getAttribute</strong>(<span class="var">'id'</span>); </div>
|
||||
</div>
|
||||
<div id="frag_traverse_traverse">
|
||||
<div class="code">You can also call methods with <a href="manual_api.htm#camel"><span class="var">Camel naming convertions</span></a>.<br>
|
||||
<table cellspacing="1" cellpadding="1">
|
||||
<tr>
|
||||
<th> Method </th>
|
||||
<th> Description</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">mixed</div>$e-><strong>children</strong> ( <span class="var">[int $index]</span> ) </td>
|
||||
<td>Returns the Nth <strong>child object</strong> if <strong>index</strong> is set, otherwise return an <strong>array of children</strong>. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">element</div>$e-><strong>parent</strong> () </td>
|
||||
<td>Returns the <strong>parent</strong> of element. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">element</div>$e-><strong>first_child</strong> () </td>
|
||||
<td>Returns the <strong>first child</strong> of element, or <strong>null</strong> if not found. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">element</div>$e-><strong>last_child</strong> () </td>
|
||||
<td>Returns the <strong>last child</strong> of element, or <strong>null</strong> if not found. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">element</div>$e-><strong>next_sibling</strong> () </td>
|
||||
<td>Returns the <strong>next sibling</strong> of element, or<strong> null</strong> if not found. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">element</div>$e-><strong>prev_sibling</strong> () </td>
|
||||
<td>Returns the <strong>previous sibling</strong> of element, or <strong>null</strong> if not found. </td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<a name="section_dump"></a>
|
||||
<h2>How to dump contents of DOM object?</h2>
|
||||
<a class="top" href="#top">Top</a>
|
||||
<div id="container_dump">
|
||||
<ul>
|
||||
<li><a href="#frag_dump_quick"><span>Quick way</span></a></li>
|
||||
<li><a href="#frag_dump_oo"><span>Object-oriented way</span></a></li>
|
||||
</ul>
|
||||
<div id="frag_dump_oo">
|
||||
<div class="code"><span class="comment">// </span><span class="comment">Dumps the internal DOM tree back into string </span><br>
|
||||
$str = $html-><strong>save</strong>();<br>
|
||||
<br>
|
||||
<span class="comment">// Dumps the internal DOM tree back into a file</span> <br>
|
||||
$html-><strong>save</strong>(<span class="var">'result.htm'</span>);</div>
|
||||
</div>
|
||||
<div id="frag_dump_quick">
|
||||
<div class="code"><span class="comment">// </span><span class="comment">Dumps the internal DOM tree back into string </span><br>
|
||||
$str = $html;<br>
|
||||
<br>
|
||||
<span class="comment">// Print it!</span><br>
|
||||
echo $html; <br>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<a name="section_callback"></a>
|
||||
<h2>How to customize the parsing behavior?</h2>
|
||||
<a class="top" href="#top">Top</a>
|
||||
<div id="container_callback">
|
||||
<ul>
|
||||
<li><a href="#frag_callback"><span>Callback function</span></a></li>
|
||||
</ul>
|
||||
<div id="frag_callback">
|
||||
<div class="code"><span class="comment">// Write a function with parameter "<strong>$element</strong>"</span><br>
|
||||
function my_callback(<span class="var">$element</span>) {<br>
|
||||
<span class="comment">// Hide all <b> tags </span><br>
|
||||
if ($element->tag==<span class="var">'b'</span>)<br>
|
||||
$element->outertext = '';<br>
|
||||
} <br>
|
||||
<br>
|
||||
<span class="comment">// Register the callback function with it's <strong>function name</strong></span><br>
|
||||
$html-><strong>set_callback</strong>(<span class="var">'my_callback'</span>);<br>
|
||||
<br>
|
||||
<span class="comment">// Callback function will be invoked while dumping</span><br>
|
||||
echo $html;
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div><br>
|
||||
Author: S.C. Chen (me578022@gmail.com)<br>
|
||||
Original idea is from Jose Solorzano's <a href="http://php-html.sourceforge.net/">HTML Parser for PHP 4</a>. <br>
|
||||
Contributions by: Contributions by: Yousuke Kumakura, Vadim Voituk, Antcs<br>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
<!--$Rev: 165 $-->
|
||||
@@ -0,0 +1,320 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
<title>PHP Simple HTML DOM Parser: API Reference</title>
|
||||
<link href="css/default.css" rel="stylesheet" type="text/css">
|
||||
</head>
|
||||
<body>
|
||||
<h1><a name="top"></a>PHP Simple HTML DOM Parser Manual</h1>
|
||||
<div id="content">
|
||||
<h2>Index</h2>
|
||||
<ul>
|
||||
<li><a href="manual.htm">Back</a></li>
|
||||
<li><a href="#api">API Reference</a></li>
|
||||
<li><a href="#camel">Camel naming conventions</a></li>
|
||||
</ul>
|
||||
<a name="api"></a>
|
||||
<h2>API Reference</h2>
|
||||
<a class="top" href="#top">Top</a>
|
||||
<div class="code"> <strong>Helper</strong> functions
|
||||
<table width="100%" cellpadding="1" cellspacing="1">
|
||||
<tr>
|
||||
<th width="320">Name</th>
|
||||
<th>Description</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><span class="returns">object</span> str_get_html ( <span class="var">string $content</span> )</td>
|
||||
<td class="description">Creates a DOM object from a string.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><span class="returns">object</span> file_get_html ( <span class="var">string $filename</span> )</td>
|
||||
<td class="description">Creates a DOM object from a file or a URL.</td>
|
||||
</tr>
|
||||
</table>
|
||||
<br>
|
||||
<strong>DOM</strong> methods & properties <br>
|
||||
<table width="100%" cellpadding="1" cellspacing="1">
|
||||
<tr>
|
||||
<th width="320"> Name</th>
|
||||
<th> Description</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">void</div>
|
||||
__construct ( <span class="var">[string $filename]</span> )</td>
|
||||
<td class="description">Constructor, set the filename parameter will automatically load the contents, either text or file/url.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">string</div>
|
||||
plaintext</td>
|
||||
<td class="description">Returns the contents extracted from HTML.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">void</div>
|
||||
clear ()</td>
|
||||
<td class="description">Clean up memory.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">void</div>
|
||||
load ( <span class="var">string $content </span>)</td>
|
||||
<td class="description"> Load contents from a <strong>string</strong>. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">string</div>
|
||||
save ( <span class="var">[string $filename]</span> )</td>
|
||||
<td class="description">Dumps the internal DOM tree back into a <strong>string</strong>. If the $filename is set, result string will save to file. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">void</div>
|
||||
load_file (<span class="var"> string $filename</span> )</td>
|
||||
<td class="description"> Load contents from a from a file or a URL.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">void</div>
|
||||
set_callback ( <span class="var">string $function_name </span>)</td>
|
||||
<td class="description">Set a callback function. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">mixed</div>
|
||||
find (<span class="var"> string $selector [, int $index] </span>)</td>
|
||||
<td class="description">Find elements by the CSS selector. Returns the Nth element <strong>object</strong> if <strong>index</strong> is set, otherwise return an <strong>array</strong> of object. </td>
|
||||
</tr>
|
||||
</table>
|
||||
<br>
|
||||
<strong>Element</strong> methods & properties <br>
|
||||
<table width="100%" cellpadding="1" cellspacing="1">
|
||||
<tr>
|
||||
<th width="320">Name</th>
|
||||
<th>Description</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">string</div>
|
||||
<span class="var">[attribute]</span></td>
|
||||
<td class="description">Read or write element's attribure value. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">string</div>
|
||||
tag</td>
|
||||
<td class="description">Read or write the tag name of element.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">string</div>
|
||||
outertext</td>
|
||||
<td class="description">Read or write the outer HTML text of element.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">string</div>
|
||||
innertext</td>
|
||||
<td class="description">Read or write the inner HTML text of element.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">string</div>
|
||||
plaintext</td>
|
||||
<td class="description">Read or write the plain text of element.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">mixed</div>
|
||||
find (<span class="var"> string $selector [, int $index] </span>)</td>
|
||||
<td class="description">Find children by the CSS selector. Returns the Nth element <strong>object</strong> if <strong>index</strong> is set, otherwise, return an <strong>array</strong> of object. </td>
|
||||
</tr>
|
||||
</table>
|
||||
<strong><br>
|
||||
DOM</strong> traversing<br>
|
||||
<table width="100%" cellpadding="1" cellspacing="1">
|
||||
<tr>
|
||||
<th width="320">Name</th>
|
||||
<th>Description</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">mixed</div>
|
||||
$e->children ( <span class="var">[int $index]</span> ) </td>
|
||||
<td class="description">Returns the Nth child <strong>object</strong> if <strong>index</strong> is set, otherwise return an <strong>array</strong> of children. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->parent () </td>
|
||||
<td class="description">Returns the parent of element. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->first_child () </td>
|
||||
<td class="description">Returns the first child of element, or <strong>null</strong> if not found. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->last_child () </td>
|
||||
<td class="description">Returns the last child of element, or <strong>null</strong> if not found. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->next_sibling () </td>
|
||||
<td class="description">Returns the next sibling of element, or<strong> null</strong> if not found. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->prev_sibling () </td>
|
||||
<td class="description">Returns the previous sibling of element, or <strong>null</strong> if not found. </td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
<a name="camel"></a>
|
||||
<h2>Camel naming convertions</h2>
|
||||
<a class="top" href="#top">Top</a>
|
||||
<div class="code">You can also call methods with W3C STANDARD camel naming convertions.<br>
|
||||
<br>
|
||||
<table width="100%" cellpadding="1" cellspacing="1">
|
||||
<tr>
|
||||
<th width="50%">Method</th>
|
||||
<th>Mapping</th>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">array</div>
|
||||
$e->getAllAttributes () </td>
|
||||
<td>
|
||||
<div class="returns">array</div>
|
||||
$e-><span class="var">attr</span></td>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">string</div>
|
||||
$e->getAttribute ( <span class="var">$name</span> ) </td>
|
||||
<td>
|
||||
<div class="returns">string</div>
|
||||
$e-><span class="var">attribute</span></td>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">void</div>
|
||||
$e->setAttribute ( <span class="var">$name, $value</span> ) </td>
|
||||
<td>
|
||||
<div class="returns">void</div>
|
||||
$value = $e-><span class="var">attribute</span></td>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">bool</div>
|
||||
$e->hasAttribute ( <span class="var">$name</span> ) </td>
|
||||
<td>
|
||||
<div class="returns">bool</div>
|
||||
isset($e-><span class="var">attribute</span>)</td>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">void</div>
|
||||
$e->removeAttribute ( <span class="var">$name</span> ) </td>
|
||||
<td>
|
||||
<div class="returns">void</div>
|
||||
$e-><span class="var">attribute</span> = null</td>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->getElementById ( <span class="var">$id</span> ) </td>
|
||||
<td>
|
||||
<div class="returns">mixed</div>
|
||||
$e->find (<span class="var"> "#$id", 0 </span>)</td>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">mixed</div>
|
||||
$e->getElementsById ( <span class="var">$id [,$index] </span> ) </td>
|
||||
<td>
|
||||
<div class="returns">mixed</div>
|
||||
$e->find (<span class="var"> "#$id" [, int $index] </span>)</td>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->getElementByTagName (<span class="var">$name</span> ) </td>
|
||||
<td>
|
||||
<div class="returns">mixed</div>
|
||||
$e->find (<span class="var"> $name, 0 </span>)</td>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">mixed</div>
|
||||
$e->getElementsByTagName ( <span class="var">$name [, $index]</span> ) </td>
|
||||
<td>
|
||||
<div class="returns">mixed</div>
|
||||
$e->find (<span class="var"> $name [, int $index] </span>)</td>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->parentNode () </td>
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->parent () </td>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">mixed</div>
|
||||
$e->childNodes ( <span class="var">[$index]</span> ) </td>
|
||||
<td>
|
||||
<div class="returns">mixed</div>
|
||||
$e->children ( <span class="var">[int $index]</span> ) </td>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->firstChild () </td>
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->first_child () </td>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->lastChild () </td>
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->last_child () </td>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->nextSibling () </td>
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->next_sibling () </td>
|
||||
</tr>
|
||||
<tr bgcolor="#EEEEEE">
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->previousSibling () </td>
|
||||
<td>
|
||||
<div class="returns">element</div>
|
||||
$e->prev_sibling () </td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
<div><br>
|
||||
Author: S.C. Chen (me578022@gmail.com)<br>
|
||||
Original idea is from Jose Solorzano's <a href="http://php-html.sourceforge.net/">HTML Parser for PHP 4</a>. <br>
|
||||
Contributions by: Contributions by: Yousuke Kumakura, Vadim Voituk, Antcs<br>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
<!--$Rev: 165 $-->
|
||||
@@ -0,0 +1,91 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
<title>PHP Simple HTML DOM Parser: FAQ</title>
|
||||
<link href="css/default.css" rel="stylesheet" type="text/css">
|
||||
</head>
|
||||
<body>
|
||||
<h1><a name="top"></a>PHP Simple HTML DOM Parser Manual</h1>
|
||||
<div id="content">
|
||||
<h2>FAQ</h2>
|
||||
<ul>
|
||||
<li><a href="manual.htm">Back</a></li>
|
||||
<li><a href="#find">Problem with finding</a></li>
|
||||
<li><a href="#hosting">Problem with hosting</a></li>
|
||||
<li><a href="#proxy">Behind a proxy</a></li>
|
||||
<li><a href="#memory_leak">Memory leak!</a></li>
|
||||
</ul>
|
||||
<div>
|
||||
<a name="find"></a>
|
||||
<h2>Problem with finding</h2>
|
||||
<a class="top" href="#top">Top</a>
|
||||
<div class="code"> <span class="var">Q:</span> Element not found in such case:<br>
|
||||
$html->find('div[style=<span class="var">padding: 0px 2px;</span>] span[class=rf]');<br>
|
||||
<br>
|
||||
<span class="var">A:</span> If there is blank in selectors, quote it! <br>
|
||||
$html->find('div[style=<strong class="var">"padding: 0px 2px;"</strong>] span[class=rf]');</div>
|
||||
<a name="hosting"></a>
|
||||
<h2>Problem with hosting</h2>
|
||||
<a class="top" href="#top">Top</a>
|
||||
<div class="code"> <span class="var">Q:</span> On my local server everything works fine, but when I put it on my esternal server it doesn't work. <br>
|
||||
<br>
|
||||
<span class="var">A:</span> The "file_get_dom" function is a wrapper of "file_get_contents" function, you must set "<strong>allow_url_fopen</strong>" as <strong>TRUE</strong> in "php.ini" to allow accessing files via HTTP or FTP. However, some hosting venders disabled PHP's "allow_url_fopen" flag for security issues... PHP provides excellent support for "curl" library to do the same job, Use curl to get the page, then call "str_get_dom" to create DOM object. <br>
|
||||
<br>
|
||||
Example: <br>
|
||||
<br>
|
||||
$curl = curl_init(); <br>
|
||||
curl_setopt(<span class="var">$curl, CURLOPT_URL, 'http://????????'</span>); <br>
|
||||
curl_setopt(<span class="var">$curl, CURLOPT_RETURNTRANSFER, 1</span>); <br>
|
||||
curl_setopt(<span class="var">$curl, CURLOPT_CONNECTTIMEOUT, 10</span>); <br>
|
||||
$str = curl_exec(<span class="var">$curl</span>); <br>
|
||||
curl_close($curl); <br>
|
||||
<br>
|
||||
$html= <strong>str_get_html</strong>($str); <br>
|
||||
... </div>
|
||||
<a name="proxy"></a>
|
||||
<div>
|
||||
<h2>Behind a proxy</h2>
|
||||
<a class="top" href="#top">Top</a>
|
||||
<div class="code"> <span class="var">Q:</span> My server is behind a Proxy and i can't use file_get_contents b/c it returns a unauthorized error.<br>
|
||||
<br>
|
||||
<span class="var">A:</span> Thanks for Shaggy to provide the solution: <br>
|
||||
<br>
|
||||
<span class="comment">// Define a context for HTTP. </span><br>
|
||||
$context = array<br>
|
||||
( <br>
|
||||
<span class="var">'http'</span> => array<br>
|
||||
( <br>
|
||||
<span class="var">'proxy'</span> => <span class="var">'addresseproxy:portproxy'</span>, <span class="comment">// This needs to be the server and the port of the NTLM Authentication Proxy Server. </span><br>
|
||||
<span class="var"> 'request_fulluri'</span> => true, <br>
|
||||
), <br>
|
||||
); <br>
|
||||
<br>
|
||||
$context = <strong>stream_context_create</strong>($context); <br>
|
||||
<br>
|
||||
$html= <strong>file_get_html</strong>(<span class="var">'http://www.php.net'</span>, <span class="var">false</span>, <span class="var">$context</span>); <br>
|
||||
...<br>
|
||||
</div>
|
||||
</div>
|
||||
<a name="memory_leak"></a>
|
||||
<h2>Memory leak!</h2>
|
||||
<a class="top" href="#top">Top</a>
|
||||
<div class="code"> <span class="var">Q:</span> This script is leaking memory seriously... After it finished running, it's not cleaning up dom object properly from memory.. <br>
|
||||
<br>
|
||||
<span class="var">A:</span> Due to php5 circular references memory leak, after creating DOM object, you must call $dom->clear() to free memory if call file_get_dom() more then once. <br>
|
||||
<br>
|
||||
Example: <br>
|
||||
<br>
|
||||
$html = file_get_html(...); <br>
|
||||
<span class="comment">// do something... </span><br>
|
||||
$html->clear(); <br>
|
||||
unset($html);</div>
|
||||
<br>
|
||||
Author: S.C. Chen (me578022@gmail.com)<br>
|
||||
Original idea is from Jose Solorzano's <a href="http://php-html.sourceforge.net/">HTML Parser for PHP 4</a>. <br>
|
||||
Contributions by: Yousuke Kumakura, Vadim Voituk, Antcs<br>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
<!--$Rev: 165 $-->
|
||||
@@ -0,0 +1,975 @@
|
||||
<?php
|
||||
/*******************************************************************************
|
||||
Version: 1.11 ($Rev: 175 $)
|
||||
Website: http://sourceforge.net/projects/simplehtmldom/
|
||||
Author: S.C. Chen <me578022@gmail.com>
|
||||
Acknowledge: Jose Solorzano (https://sourceforge.net/projects/php-html/)
|
||||
Contributions by:
|
||||
Yousuke Kumakura (Attribute filters)
|
||||
Vadim Voituk (Negative indexes supports of "find" method)
|
||||
Antcs (Constructor with automatically load contents either text or file/url)
|
||||
Licensed under The MIT License
|
||||
Redistributions of files must retain the above copyright notice.
|
||||
*******************************************************************************/
|
||||
|
||||
define('HDOM_TYPE_ELEMENT', 1);
|
||||
define('HDOM_TYPE_COMMENT', 2);
|
||||
define('HDOM_TYPE_TEXT', 3);
|
||||
define('HDOM_TYPE_ENDTAG', 4);
|
||||
define('HDOM_TYPE_ROOT', 5);
|
||||
define('HDOM_TYPE_UNKNOWN', 6);
|
||||
define('HDOM_QUOTE_DOUBLE', 0);
|
||||
define('HDOM_QUOTE_SINGLE', 1);
|
||||
define('HDOM_QUOTE_NO', 3);
|
||||
define('HDOM_INFO_BEGIN', 0);
|
||||
define('HDOM_INFO_END', 1);
|
||||
define('HDOM_INFO_QUOTE', 2);
|
||||
define('HDOM_INFO_SPACE', 3);
|
||||
define('HDOM_INFO_TEXT', 4);
|
||||
define('HDOM_INFO_INNER', 5);
|
||||
define('HDOM_INFO_OUTER', 6);
|
||||
define('HDOM_INFO_ENDSPACE',7);
|
||||
|
||||
// helper functions
|
||||
// -----------------------------------------------------------------------------
|
||||
// get html dom form file
|
||||
function file_get_html() {
|
||||
$dom = new simple_html_dom;
|
||||
$args = func_get_args();
|
||||
$dom->load(call_user_func_array('file_get_contents', $args), true);
|
||||
return $dom;
|
||||
}
|
||||
|
||||
// get html dom form string
|
||||
function str_get_html($str, $lowercase=true) {
|
||||
$dom = new simple_html_dom;
|
||||
$dom->load($str, $lowercase);
|
||||
return $dom;
|
||||
}
|
||||
|
||||
// dump html dom tree
|
||||
function dump_html_tree($node, $show_attr=true, $deep=0) {
|
||||
$lead = str_repeat(' ', $deep);
|
||||
echo $lead.$node->tag;
|
||||
if ($show_attr && count($node->attr)>0) {
|
||||
echo '(';
|
||||
foreach($node->attr as $k=>$v)
|
||||
echo "[$k]=>\"".$node->$k.'", ';
|
||||
echo ')';
|
||||
}
|
||||
echo "\n";
|
||||
|
||||
foreach($node->nodes as $c)
|
||||
dump_html_tree($c, $show_attr, $deep+1);
|
||||
}
|
||||
|
||||
// get dom form file (deprecated)
|
||||
function file_get_dom() {
|
||||
$dom = new simple_html_dom;
|
||||
$args = func_get_args();
|
||||
$dom->load(call_user_func_array('file_get_contents', $args), true);
|
||||
return $dom;
|
||||
}
|
||||
|
||||
// get dom form string (deprecated)
|
||||
function str_get_dom($str, $lowercase=true) {
|
||||
$dom = new simple_html_dom;
|
||||
$dom->load($str, $lowercase);
|
||||
return $dom;
|
||||
}
|
||||
|
||||
// simple html dom node
|
||||
// -----------------------------------------------------------------------------
|
||||
class simple_html_dom_node {
|
||||
public $nodetype = HDOM_TYPE_TEXT;
|
||||
public $tag = 'text';
|
||||
public $attr = array();
|
||||
public $children = array();
|
||||
public $nodes = array();
|
||||
public $parent = null;
|
||||
public $_ = array();
|
||||
private $dom = null;
|
||||
|
||||
function __construct($dom) {
|
||||
$this->dom = $dom;
|
||||
$dom->nodes[] = $this;
|
||||
}
|
||||
|
||||
function __destruct() {
|
||||
$this->clear();
|
||||
}
|
||||
|
||||
function __toString() {
|
||||
return $this->outertext();
|
||||
}
|
||||
|
||||
// clean up memory due to php5 circular references memory leak...
|
||||
function clear() {
|
||||
$this->dom = null;
|
||||
$this->nodes = null;
|
||||
$this->parent = null;
|
||||
$this->children = null;
|
||||
}
|
||||
|
||||
// dump node's tree
|
||||
function dump($show_attr=true) {
|
||||
dump_html_tree($this, $show_attr);
|
||||
}
|
||||
|
||||
// returns the parent of node
|
||||
function parent() {
|
||||
return $this->parent;
|
||||
}
|
||||
|
||||
// returns children of node
|
||||
function children($idx=-1) {
|
||||
if ($idx===-1) return $this->children;
|
||||
if (isset($this->children[$idx])) return $this->children[$idx];
|
||||
return null;
|
||||
}
|
||||
|
||||
// returns the first child of node
|
||||
function first_child() {
|
||||
if (count($this->children)>0) return $this->children[0];
|
||||
return null;
|
||||
}
|
||||
|
||||
// returns the last child of node
|
||||
function last_child() {
|
||||
if (($count=count($this->children))>0) return $this->children[$count-1];
|
||||
return null;
|
||||
}
|
||||
|
||||
// returns the next sibling of node
|
||||
function next_sibling() {
|
||||
if ($this->parent===null) return null;
|
||||
$idx = 0;
|
||||
$count = count($this->parent->children);
|
||||
while ($idx<$count && $this!==$this->parent->children[$idx])
|
||||
++$idx;
|
||||
if (++$idx>=$count) return null;
|
||||
return $this->parent->children[$idx];
|
||||
}
|
||||
|
||||
// returns the previous sibling of node
|
||||
function prev_sibling() {
|
||||
if ($this->parent===null) return null;
|
||||
$idx = 0;
|
||||
$count = count($this->parent->children);
|
||||
while ($idx<$count && $this!==$this->parent->children[$idx])
|
||||
++$idx;
|
||||
if (--$idx<0) return null;
|
||||
return $this->parent->children[$idx];
|
||||
}
|
||||
|
||||
// get dom node's inner html
|
||||
function innertext() {
|
||||
if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER];
|
||||
if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);
|
||||
|
||||
$ret = '';
|
||||
foreach($this->nodes as $n)
|
||||
$ret .= $n->outertext();
|
||||
return $ret;
|
||||
}
|
||||
|
||||
// get dom node's outer text (with tag)
|
||||
function outertext() {
|
||||
if ($this->tag==='root') return $this->innertext();
|
||||
|
||||
// trigger callback
|
||||
if ($this->dom->callback!==null)
|
||||
call_user_func_array($this->dom->callback, array($this));
|
||||
|
||||
if (isset($this->_[HDOM_INFO_OUTER])) return $this->_[HDOM_INFO_OUTER];
|
||||
if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);
|
||||
|
||||
// render begin tag
|
||||
$ret = $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]]->makeup();
|
||||
|
||||
// render inner text
|
||||
if (isset($this->_[HDOM_INFO_INNER]))
|
||||
$ret .= $this->_[HDOM_INFO_INNER];
|
||||
else {
|
||||
foreach($this->nodes as $n)
|
||||
$ret .= $n->outertext();
|
||||
}
|
||||
|
||||
// render end tag
|
||||
if(isset($this->_[HDOM_INFO_END]) && $this->_[HDOM_INFO_END]!=0)
|
||||
$ret .= '</'.$this->tag.'>';
|
||||
return $ret;
|
||||
}
|
||||
|
||||
// get dom node's plain text
|
||||
function text() {
|
||||
if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER];
|
||||
switch ($this->nodetype) {
|
||||
case HDOM_TYPE_TEXT: return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);
|
||||
case HDOM_TYPE_COMMENT: return '';
|
||||
case HDOM_TYPE_UNKNOWN: return '';
|
||||
}
|
||||
if (strcasecmp($this->tag, 'script')===0) return '';
|
||||
if (strcasecmp($this->tag, 'style')===0) return '';
|
||||
|
||||
$ret = '';
|
||||
foreach($this->nodes as $n)
|
||||
$ret .= $n->text();
|
||||
return $ret;
|
||||
}
|
||||
|
||||
function xmltext() {
|
||||
$ret = $this->innertext();
|
||||
$ret = str_ireplace('<![CDATA[', '', $ret);
|
||||
$ret = str_replace(']]>', '', $ret);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
// build node's text with tag
|
||||
function makeup() {
|
||||
// text, comment, unknown
|
||||
if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);
|
||||
|
||||
$ret = '<'.$this->tag;
|
||||
$i = -1;
|
||||
|
||||
foreach($this->attr as $key=>$val) {
|
||||
++$i;
|
||||
|
||||
// skip removed attribute
|
||||
if ($val===null || $val===false)
|
||||
continue;
|
||||
|
||||
$ret .= $this->_[HDOM_INFO_SPACE][$i][0];
|
||||
//no value attr: nowrap, checked selected...
|
||||
if ($val===true)
|
||||
$ret .= $key;
|
||||
else {
|
||||
switch($this->_[HDOM_INFO_QUOTE][$i]) {
|
||||
case HDOM_QUOTE_DOUBLE: $quote = '"'; break;
|
||||
case HDOM_QUOTE_SINGLE: $quote = '\''; break;
|
||||
default: $quote = '';
|
||||
}
|
||||
$ret .= $key.$this->_[HDOM_INFO_SPACE][$i][1].'='.$this->_[HDOM_INFO_SPACE][$i][2].$quote.$val.$quote;
|
||||
}
|
||||
}
|
||||
$ret = $this->dom->restore_noise($ret);
|
||||
return $ret . $this->_[HDOM_INFO_ENDSPACE] . '>';
|
||||
}
|
||||
|
||||
// find elements by css selector
|
||||
function find($selector, $idx=null) {
|
||||
$selectors = $this->parse_selector($selector);
|
||||
if (($count=count($selectors))===0) return array();
|
||||
$found_keys = array();
|
||||
|
||||
// find each selector
|
||||
for ($c=0; $c<$count; ++$c) {
|
||||
if (($levle=count($selectors[0]))===0) return array();
|
||||
if (!isset($this->_[HDOM_INFO_BEGIN])) return array();
|
||||
|
||||
$head = array($this->_[HDOM_INFO_BEGIN]=>1);
|
||||
|
||||
// handle descendant selectors, no recursive!
|
||||
for ($l=0; $l<$levle; ++$l) {
|
||||
$ret = array();
|
||||
foreach($head as $k=>$v) {
|
||||
$n = ($k===-1) ? $this->dom->root : $this->dom->nodes[$k];
|
||||
$n->seek($selectors[$c][$l], $ret);
|
||||
}
|
||||
$head = $ret;
|
||||
}
|
||||
|
||||
foreach($head as $k=>$v) {
|
||||
if (!isset($found_keys[$k]))
|
||||
$found_keys[$k] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// sort keys
|
||||
ksort($found_keys);
|
||||
|
||||
$found = array();
|
||||
foreach($found_keys as $k=>$v)
|
||||
$found[] = $this->dom->nodes[$k];
|
||||
|
||||
// return nth-element or array
|
||||
if (is_null($idx)) return $found;
|
||||
else if ($idx<0) $idx = count($found) + $idx;
|
||||
return (isset($found[$idx])) ? $found[$idx] : null;
|
||||
}
|
||||
|
||||
// seek for given conditions
|
||||
protected function seek($selector, &$ret) {
|
||||
list($tag, $key, $val, $exp, $no_key) = $selector;
|
||||
|
||||
// xpath index
|
||||
if ($tag && $key && is_numeric($key)) {
|
||||
$count = 0;
|
||||
foreach ($this->children as $c) {
|
||||
if ($tag==='*' || $tag===$c->tag) {
|
||||
if (++$count==$key) {
|
||||
$ret[$c->_[HDOM_INFO_BEGIN]] = 1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
$end = (!empty($this->_[HDOM_INFO_END])) ? $this->_[HDOM_INFO_END] : 0;
|
||||
if ($end==0) {
|
||||
$parent = $this->parent;
|
||||
while (!isset($parent->_[HDOM_INFO_END]) && $parent!==null) {
|
||||
$end -= 1;
|
||||
$parent = $parent->parent;
|
||||
}
|
||||
$end += $parent->_[HDOM_INFO_END];
|
||||
}
|
||||
|
||||
for($i=$this->_[HDOM_INFO_BEGIN]+1; $i<$end; ++$i) {
|
||||
$node = $this->dom->nodes[$i];
|
||||
$pass = true;
|
||||
|
||||
if ($tag==='*' && !$key) {
|
||||
if (in_array($node, $this->children, true))
|
||||
$ret[$i] = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
// compare tag
|
||||
if ($tag && $tag!=$node->tag && $tag!=='*') {$pass=false;}
|
||||
// compare key
|
||||
if ($pass && $key) {
|
||||
if ($no_key) {
|
||||
if (isset($node->attr[$key])) $pass=false;
|
||||
}
|
||||
else if (!isset($node->attr[$key])) $pass=false;
|
||||
}
|
||||
// compare value
|
||||
if ($pass && $key && $val && $val!=='*') {
|
||||
$check = $this->match($exp, $val, $node->attr[$key]);
|
||||
// handle multiple class
|
||||
if (!$check && strcasecmp($key, 'class')===0) {
|
||||
foreach(explode(' ',$node->attr[$key]) as $k) {
|
||||
$check = $this->match($exp, $val, $k);
|
||||
if ($check) break;
|
||||
}
|
||||
}
|
||||
if (!$check) $pass = false;
|
||||
}
|
||||
if ($pass) $ret[$i] = 1;
|
||||
unset($node);
|
||||
}
|
||||
}
|
||||
|
||||
protected function match($exp, $pattern, $value) {
|
||||
switch ($exp) {
|
||||
case '=':
|
||||
return ($value===$pattern);
|
||||
case '!=':
|
||||
return ($value!==$pattern);
|
||||
case '^=':
|
||||
return preg_match("/^".preg_quote($pattern,'/')."/", $value);
|
||||
case '$=':
|
||||
return preg_match("/".preg_quote($pattern,'/')."$/", $value);
|
||||
case '*=':
|
||||
if ($pattern[0]=='/')
|
||||
return preg_match($pattern, $value);
|
||||
return preg_match("/".$pattern."/i", $value);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function parse_selector($selector_string) {
|
||||
// pattern of CSS selectors, modified from mootools
|
||||
$pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is";
|
||||
preg_match_all($pattern, trim($selector_string).' ', $matches, PREG_SET_ORDER);
|
||||
$selectors = array();
|
||||
$result = array();
|
||||
//print_r($matches);
|
||||
|
||||
foreach ($matches as $m) {
|
||||
$m[0] = trim($m[0]);
|
||||
if ($m[0]==='' || $m[0]==='/' || $m[0]==='//') continue;
|
||||
// for borwser grnreated xpath
|
||||
if ($m[1]==='tbody') continue;
|
||||
|
||||
list($tag, $key, $val, $exp, $no_key) = array($m[1], null, null, '=', false);
|
||||
if(!empty($m[2])) {$key='id'; $val=$m[2];}
|
||||
if(!empty($m[3])) {$key='class'; $val=$m[3];}
|
||||
if(!empty($m[4])) {$key=$m[4];}
|
||||
if(!empty($m[5])) {$exp=$m[5];}
|
||||
if(!empty($m[6])) {$val=$m[6];}
|
||||
|
||||
// convert to lowercase
|
||||
if ($this->dom->lowercase) {$tag=strtolower($tag); $key=strtolower($key);}
|
||||
//elements that do NOT have the specified attribute
|
||||
if (isset($key[0]) && $key[0]==='!') {$key=substr($key, 1); $no_key=true;}
|
||||
|
||||
$result[] = array($tag, $key, $val, $exp, $no_key);
|
||||
if (trim($m[7])===',') {
|
||||
$selectors[] = $result;
|
||||
$result = array();
|
||||
}
|
||||
}
|
||||
if (count($result)>0)
|
||||
$selectors[] = $result;
|
||||
return $selectors;
|
||||
}
|
||||
|
||||
function __get($name) {
|
||||
if (isset($this->attr[$name])) return $this->attr[$name];
|
||||
switch($name) {
|
||||
case 'outertext': return $this->outertext();
|
||||
case 'innertext': return $this->innertext();
|
||||
case 'plaintext': return $this->text();
|
||||
case 'xmltext': return $this->xmltext();
|
||||
default: return array_key_exists($name, $this->attr);
|
||||
}
|
||||
}
|
||||
|
||||
function __set($name, $value) {
|
||||
switch($name) {
|
||||
case 'outertext': return $this->_[HDOM_INFO_OUTER] = $value;
|
||||
case 'innertext':
|
||||
if (isset($this->_[HDOM_INFO_TEXT])) return $this->_[HDOM_INFO_TEXT] = $value;
|
||||
return $this->_[HDOM_INFO_INNER] = $value;
|
||||
}
|
||||
if (!isset($this->attr[$name])) {
|
||||
$this->_[HDOM_INFO_SPACE][] = array(' ', '', '');
|
||||
$this->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE;
|
||||
}
|
||||
$this->attr[$name] = $value;
|
||||
}
|
||||
|
||||
function __isset($name) {
|
||||
switch($name) {
|
||||
case 'outertext': return true;
|
||||
case 'innertext': return true;
|
||||
case 'plaintext': return true;
|
||||
}
|
||||
//no value attr: nowrap, checked selected...
|
||||
return (array_key_exists($name, $this->attr)) ? true : isset($this->attr[$name]);
|
||||
}
|
||||
|
||||
function __unset($name) {
|
||||
if (isset($this->attr[$name]))
|
||||
unset($this->attr[$name]);
|
||||
}
|
||||
|
||||
// camel naming conventions
|
||||
function getAllAttributes() {return $this->attr;}
|
||||
function getAttribute($name) {return $this->__get($name);}
|
||||
function setAttribute($name, $value) {$this->__set($name, $value);}
|
||||
function hasAttribute($name) {return $this->__isset($name);}
|
||||
function removeAttribute($name) {$this->__set($name, null);}
|
||||
function getElementById($id) {return $this->find("#$id", 0);}
|
||||
function getElementsById($id, $idx=null) {return $this->find("#$id", $idx);}
|
||||
function getElementByTagName($name) {return $this->find($name, 0);}
|
||||
function getElementsByTagName($name, $idx=null) {return $this->find($name, $idx);}
|
||||
function parentNode() {return $this->parent();}
|
||||
function childNodes($idx=-1) {return $this->children($idx);}
|
||||
function firstChild() {return $this->first_child();}
|
||||
function lastChild() {return $this->last_child();}
|
||||
function nextSibling() {return $this->next_sibling();}
|
||||
function previousSibling() {return $this->prev_sibling();}
|
||||
}
|
||||
|
||||
// simple html dom parser
|
||||
// -----------------------------------------------------------------------------
|
||||
class simple_html_dom {
|
||||
public $root = null;
|
||||
public $nodes = array();
|
||||
public $callback = null;
|
||||
public $lowercase = false;
|
||||
protected $pos;
|
||||
protected $doc;
|
||||
protected $char;
|
||||
protected $size;
|
||||
protected $cursor;
|
||||
protected $parent;
|
||||
protected $noise = array();
|
||||
protected $token_blank = " \t\r\n";
|
||||
protected $token_equal = ' =/>';
|
||||
protected $token_slash = " />\r\n\t";
|
||||
protected $token_attr = ' >';
|
||||
// use isset instead of in_array, performance boost about 30%...
|
||||
protected $self_closing_tags = array('img'=>1, 'br'=>1, 'input'=>1, 'meta'=>1, 'link'=>1, 'hr'=>1, 'base'=>1, 'embed'=>1, 'spacer'=>1);
|
||||
protected $block_tags = array('root'=>1, 'body'=>1, 'form'=>1, 'div'=>1, 'span'=>1, 'table'=>1);
|
||||
protected $optional_closing_tags = array(
|
||||
'tr'=>array('tr'=>1, 'td'=>1, 'th'=>1),
|
||||
'th'=>array('th'=>1),
|
||||
'td'=>array('td'=>1),
|
||||
'li'=>array('li'=>1),
|
||||
'dt'=>array('dt'=>1, 'dd'=>1),
|
||||
'dd'=>array('dd'=>1, 'dt'=>1),
|
||||
'dl'=>array('dd'=>1, 'dt'=>1),
|
||||
'p'=>array('p'=>1),
|
||||
'nobr'=>array('nobr'=>1),
|
||||
);
|
||||
|
||||
function __construct($str=null) {
|
||||
if ($str) {
|
||||
if (preg_match("/^http:\/\//i",$str) || is_file($str))
|
||||
$this->load_file($str);
|
||||
else
|
||||
$this->load($str);
|
||||
}
|
||||
}
|
||||
|
||||
function __destruct() {
|
||||
$this->clear();
|
||||
}
|
||||
|
||||
// load html from string
|
||||
function load($str, $lowercase=true) {
|
||||
// prepare
|
||||
$this->prepare($str, $lowercase);
|
||||
// strip out comments
|
||||
$this->remove_noise("'<!--(.*?)-->'is");
|
||||
// strip out cdata
|
||||
$this->remove_noise("'<!\[CDATA\[(.*?)\]\]>'is", true);
|
||||
// strip out <style> tags
|
||||
$this->remove_noise("'<\s*style[^>]*[^/]>(.*?)<\s*/\s*style\s*>'is");
|
||||
$this->remove_noise("'<\s*style\s*>(.*?)<\s*/\s*style\s*>'is");
|
||||
// strip out <script> tags
|
||||
$this->remove_noise("'<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>'is");
|
||||
$this->remove_noise("'<\s*script\s*>(.*?)<\s*/\s*script\s*>'is");
|
||||
// strip out preformatted tags
|
||||
$this->remove_noise("'<\s*(?:code)[^>]*>(.*?)<\s*/\s*(?:code)\s*>'is");
|
||||
// strip out server side scripts
|
||||
$this->remove_noise("'(<\?)(.*?)(\?>)'s", true);
|
||||
// strip smarty scripts
|
||||
$this->remove_noise("'(\{\w)(.*?)(\})'s", true);
|
||||
|
||||
// parsing
|
||||
while ($this->parse());
|
||||
// end
|
||||
$this->root->_[HDOM_INFO_END] = $this->cursor;
|
||||
}
|
||||
|
||||
// load html from file
|
||||
function load_file() {
|
||||
$args = func_get_args();
|
||||
$this->load(call_user_func_array('file_get_contents', $args), true);
|
||||
}
|
||||
|
||||
// set callback function
|
||||
function set_callback($function_name) {
|
||||
$this->callback = $function_name;
|
||||
}
|
||||
|
||||
// remove callback function
|
||||
function remove_callback() {
|
||||
$this->callback = null;
|
||||
}
|
||||
|
||||
// save dom as string
|
||||
function save($filepath='') {
|
||||
$ret = $this->root->innertext();
|
||||
if ($filepath!=='') file_put_contents($filepath, $ret);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
// find dom node by css selector
|
||||
function find($selector, $idx=null) {
|
||||
return $this->root->find($selector, $idx);
|
||||
}
|
||||
|
||||
// clean up memory due to php5 circular references memory leak...
|
||||
function clear() {
|
||||
foreach($this->nodes as $n) {$n->clear(); $n = null;}
|
||||
if (isset($this->parent)) {$this->parent->clear(); unset($this->parent);}
|
||||
if (isset($this->root)) {$this->root->clear(); unset($this->root);}
|
||||
unset($this->doc);
|
||||
unset($this->noise);
|
||||
}
|
||||
|
||||
function dump($show_attr=true) {
|
||||
$this->root->dump($show_attr);
|
||||
}
|
||||
|
||||
// prepare HTML data and init everything
|
||||
protected function prepare($str, $lowercase=true) {
|
||||
$this->clear();
|
||||
$this->doc = $str;
|
||||
$this->pos = 0;
|
||||
$this->cursor = 1;
|
||||
$this->noise = array();
|
||||
$this->nodes = array();
|
||||
$this->lowercase = $lowercase;
|
||||
$this->root = new simple_html_dom_node($this);
|
||||
$this->root->tag = 'root';
|
||||
$this->root->_[HDOM_INFO_BEGIN] = -1;
|
||||
$this->root->nodetype = HDOM_TYPE_ROOT;
|
||||
$this->parent = $this->root;
|
||||
// set the length of content
|
||||
$this->size = strlen($str);
|
||||
if ($this->size>0) $this->char = $this->doc[0];
|
||||
}
|
||||
|
||||
// parse html content
|
||||
protected function parse() {
|
||||
if (($s = $this->copy_until_char('<'))==='')
|
||||
return $this->read_tag();
|
||||
|
||||
// text
|
||||
$node = new simple_html_dom_node($this);
|
||||
++$this->cursor;
|
||||
$node->_[HDOM_INFO_TEXT] = $s;
|
||||
$this->link_nodes($node, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
// read tag info
|
||||
protected function read_tag() {
|
||||
if ($this->char!=='<') {
|
||||
$this->root->_[HDOM_INFO_END] = $this->cursor;
|
||||
return false;
|
||||
}
|
||||
$begin_tag_pos = $this->pos;
|
||||
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
|
||||
// end tag
|
||||
if ($this->char==='/') {
|
||||
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
$this->skip($this->token_blank_t);
|
||||
$tag = $this->copy_until_char('>');
|
||||
|
||||
// skip attributes in end tag
|
||||
if (($pos = strpos($tag, ' '))!==false)
|
||||
$tag = substr($tag, 0, $pos);
|
||||
|
||||
$parent_lower = strtolower($this->parent->tag);
|
||||
$tag_lower = strtolower($tag);
|
||||
|
||||
if ($parent_lower!==$tag_lower) {
|
||||
if (isset($this->optional_closing_tags[$parent_lower]) && isset($this->block_tags[$tag_lower])) {
|
||||
$this->parent->_[HDOM_INFO_END] = 0;
|
||||
$org_parent = $this->parent;
|
||||
|
||||
while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower)
|
||||
$this->parent = $this->parent->parent;
|
||||
|
||||
if (strtolower($this->parent->tag)!==$tag_lower) {
|
||||
$this->parent = $org_parent; // restore origonal parent
|
||||
if ($this->parent->parent) $this->parent = $this->parent->parent;
|
||||
$this->parent->_[HDOM_INFO_END] = $this->cursor;
|
||||
return $this->as_text_node($tag);
|
||||
}
|
||||
}
|
||||
else if (($this->parent->parent) && isset($this->block_tags[$tag_lower])) {
|
||||
$this->parent->_[HDOM_INFO_END] = 0;
|
||||
$org_parent = $this->parent;
|
||||
|
||||
while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower)
|
||||
$this->parent = $this->parent->parent;
|
||||
|
||||
if (strtolower($this->parent->tag)!==$tag_lower) {
|
||||
$this->parent = $org_parent; // restore origonal parent
|
||||
$this->parent->_[HDOM_INFO_END] = $this->cursor;
|
||||
return $this->as_text_node($tag);
|
||||
}
|
||||
}
|
||||
else if (($this->parent->parent) && strtolower($this->parent->parent->tag)===$tag_lower) {
|
||||
$this->parent->_[HDOM_INFO_END] = 0;
|
||||
$this->parent = $this->parent->parent;
|
||||
}
|
||||
else
|
||||
return $this->as_text_node($tag);
|
||||
}
|
||||
|
||||
$this->parent->_[HDOM_INFO_END] = $this->cursor;
|
||||
if ($this->parent->parent) $this->parent = $this->parent->parent;
|
||||
|
||||
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
return true;
|
||||
}
|
||||
|
||||
$node = new simple_html_dom_node($this);
|
||||
$node->_[HDOM_INFO_BEGIN] = $this->cursor;
|
||||
++$this->cursor;
|
||||
$tag = $this->copy_until($this->token_slash);
|
||||
|
||||
// doctype, cdata & comments...
|
||||
if (isset($tag[0]) && $tag[0]==='!') {
|
||||
$node->_[HDOM_INFO_TEXT] = '<' . $tag . $this->copy_until_char('>');
|
||||
|
||||
if (isset($tag[2]) && $tag[1]==='-' && $tag[2]==='-') {
|
||||
$node->nodetype = HDOM_TYPE_COMMENT;
|
||||
$node->tag = 'comment';
|
||||
} else {
|
||||
$node->nodetype = HDOM_TYPE_UNKNOWN;
|
||||
$node->tag = 'unknown';
|
||||
}
|
||||
|
||||
if ($this->char==='>') $node->_[HDOM_INFO_TEXT].='>';
|
||||
$this->link_nodes($node, true);
|
||||
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
return true;
|
||||
}
|
||||
|
||||
// text
|
||||
if ($pos=strpos($tag, '<')!==false) {
|
||||
$tag = '<' . substr($tag, 0, -1);
|
||||
$node->_[HDOM_INFO_TEXT] = $tag;
|
||||
$this->link_nodes($node, false);
|
||||
$this->char = $this->doc[--$this->pos]; // prev
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!preg_match("/^[\w-:]+$/", $tag)) {
|
||||
$node->_[HDOM_INFO_TEXT] = '<' . $tag . $this->copy_until('<>');
|
||||
if ($this->char==='<') {
|
||||
$this->link_nodes($node, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->char==='>') $node->_[HDOM_INFO_TEXT].='>';
|
||||
$this->link_nodes($node, false);
|
||||
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
return true;
|
||||
}
|
||||
|
||||
// begin tag
|
||||
$node->nodetype = HDOM_TYPE_ELEMENT;
|
||||
$tag_lower = strtolower($tag);
|
||||
$node->tag = ($this->lowercase) ? $tag_lower : $tag;
|
||||
|
||||
// handle optional closing tags
|
||||
if (isset($this->optional_closing_tags[$tag_lower]) ) {
|
||||
while (isset($this->optional_closing_tags[$tag_lower][strtolower($this->parent->tag)])) {
|
||||
$this->parent->_[HDOM_INFO_END] = 0;
|
||||
$this->parent = $this->parent->parent;
|
||||
}
|
||||
$node->parent = $this->parent;
|
||||
}
|
||||
|
||||
$guard = 0; // prevent infinity loop
|
||||
$space = array($this->copy_skip($this->token_blank), '', '');
|
||||
|
||||
// attributes
|
||||
do {
|
||||
if ($this->char!==null && $space[0]==='') break;
|
||||
$name = $this->copy_until($this->token_equal);
|
||||
if($guard===$this->pos) {
|
||||
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
continue;
|
||||
}
|
||||
$guard = $this->pos;
|
||||
|
||||
// handle endless '<'
|
||||
if($this->pos>=$this->size-1 && $this->char!=='>') {
|
||||
$node->nodetype = HDOM_TYPE_TEXT;
|
||||
$node->_[HDOM_INFO_END] = 0;
|
||||
$node->_[HDOM_INFO_TEXT] = '<'.$tag . $space[0] . $name;
|
||||
$node->tag = 'text';
|
||||
$this->link_nodes($node, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
// handle mismatch '<'
|
||||
if($this->doc[$this->pos-1]=='<') {
|
||||
$node->nodetype = HDOM_TYPE_TEXT;
|
||||
$node->tag = 'text';
|
||||
$node->attr = array();
|
||||
$node->_[HDOM_INFO_END] = 0;
|
||||
$node->_[HDOM_INFO_TEXT] = substr($this->doc, $begin_tag_pos, $this->pos-$begin_tag_pos-1);
|
||||
$this->pos -= 2;
|
||||
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
$this->link_nodes($node, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($name!=='/' && $name!=='') {
|
||||
$space[1] = $this->copy_skip($this->token_blank);
|
||||
$name = $this->restore_noise($name);
|
||||
if ($this->lowercase) $name = strtolower($name);
|
||||
if ($this->char==='=') {
|
||||
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
$this->parse_attr($node, $name, $space);
|
||||
}
|
||||
else {
|
||||
//no value attr: nowrap, checked selected...
|
||||
$node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO;
|
||||
$node->attr[$name] = true;
|
||||
if ($this->char!='>') $this->char = $this->doc[--$this->pos]; // prev
|
||||
}
|
||||
$node->_[HDOM_INFO_SPACE][] = $space;
|
||||
$space = array($this->copy_skip($this->token_blank), '', '');
|
||||
}
|
||||
else
|
||||
break;
|
||||
} while($this->char!=='>' && $this->char!=='/');
|
||||
|
||||
$this->link_nodes($node, true);
|
||||
$node->_[HDOM_INFO_ENDSPACE] = $space[0];
|
||||
|
||||
// check self closing
|
||||
if ($this->copy_until_char_escape('>')==='/') {
|
||||
$node->_[HDOM_INFO_ENDSPACE] .= '/';
|
||||
$node->_[HDOM_INFO_END] = 0;
|
||||
}
|
||||
else {
|
||||
// reset parent
|
||||
if (!isset($this->self_closing_tags[strtolower($node->tag)])) $this->parent = $node;
|
||||
}
|
||||
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
return true;
|
||||
}
|
||||
|
||||
// parse attributes
|
||||
protected function parse_attr($node, $name, &$space) {
|
||||
$space[2] = $this->copy_skip($this->token_blank);
|
||||
switch($this->char) {
|
||||
case '"':
|
||||
$node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE;
|
||||
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
$node->attr[$name] = $this->restore_noise($this->copy_until_char_escape('"'));
|
||||
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
break;
|
||||
case '\'':
|
||||
$node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_SINGLE;
|
||||
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
$node->attr[$name] = $this->restore_noise($this->copy_until_char_escape('\''));
|
||||
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
break;
|
||||
default:
|
||||
$node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO;
|
||||
$node->attr[$name] = $this->restore_noise($this->copy_until($this->token_attr));
|
||||
}
|
||||
}
|
||||
|
||||
// link node's parent
|
||||
protected function link_nodes(&$node, $is_child) {
|
||||
$node->parent = $this->parent;
|
||||
$this->parent->nodes[] = $node;
|
||||
if ($is_child)
|
||||
$this->parent->children[] = $node;
|
||||
}
|
||||
|
||||
// as a text node
|
||||
protected function as_text_node($tag) {
|
||||
$node = new simple_html_dom_node($this);
|
||||
++$this->cursor;
|
||||
$node->_[HDOM_INFO_TEXT] = '</' . $tag . '>';
|
||||
$this->link_nodes($node, false);
|
||||
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function skip($chars) {
|
||||
$this->pos += strspn($this->doc, $chars, $this->pos);
|
||||
$this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
}
|
||||
|
||||
protected function copy_skip($chars) {
|
||||
$pos = $this->pos;
|
||||
$len = strspn($this->doc, $chars, $pos);
|
||||
$this->pos += $len;
|
||||
$this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
if ($len===0) return '';
|
||||
return substr($this->doc, $pos, $len);
|
||||
}
|
||||
|
||||
protected function copy_until($chars) {
|
||||
$pos = $this->pos;
|
||||
$len = strcspn($this->doc, $chars, $pos);
|
||||
$this->pos += $len;
|
||||
$this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
||||
return substr($this->doc, $pos, $len);
|
||||
}
|
||||
|
||||
protected function copy_until_char($char) {
|
||||
if ($this->char===null) return '';
|
||||
|
||||
if (($pos = strpos($this->doc, $char, $this->pos))===false) {
|
||||
$ret = substr($this->doc, $this->pos, $this->size-$this->pos);
|
||||
$this->char = null;
|
||||
$this->pos = $this->size;
|
||||
return $ret;
|
||||
}
|
||||
|
||||
if ($pos===$this->pos) return '';
|
||||
$pos_old = $this->pos;
|
||||
$this->char = $this->doc[$pos];
|
||||
$this->pos = $pos;
|
||||
return substr($this->doc, $pos_old, $pos-$pos_old);
|
||||
}
|
||||
|
||||
protected function copy_until_char_escape($char) {
|
||||
if ($this->char===null) return '';
|
||||
|
||||
$start = $this->pos;
|
||||
while(1) {
|
||||
if (($pos = strpos($this->doc, $char, $start))===false) {
|
||||
$ret = substr($this->doc, $this->pos, $this->size-$this->pos);
|
||||
$this->char = null;
|
||||
$this->pos = $this->size;
|
||||
return $ret;
|
||||
}
|
||||
|
||||
if ($pos===$this->pos) return '';
|
||||
|
||||
if ($this->doc[$pos-1]==='\\') {
|
||||
$start = $pos+1;
|
||||
continue;
|
||||
}
|
||||
|
||||
$pos_old = $this->pos;
|
||||
$this->char = $this->doc[$pos];
|
||||
$this->pos = $pos;
|
||||
return substr($this->doc, $pos_old, $pos-$pos_old);
|
||||
}
|
||||
}
|
||||
|
||||
// remove noise from html content
|
||||
protected function remove_noise($pattern, $remove_tag=false) {
|
||||
$count = preg_match_all($pattern, $this->doc, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE);
|
||||
|
||||
for ($i=$count-1; $i>-1; --$i) {
|
||||
$key = '___noise___'.sprintf('% 3d', count($this->noise)+100);
|
||||
$idx = ($remove_tag) ? 0 : 1;
|
||||
$this->noise[$key] = $matches[$i][$idx][0];
|
||||
$this->doc = substr_replace($this->doc, $key, $matches[$i][$idx][1], strlen($matches[$i][$idx][0]));
|
||||
}
|
||||
|
||||
// reset the length of content
|
||||
$this->size = strlen($this->doc);
|
||||
if ($this->size>0) $this->char = $this->doc[0];
|
||||
}
|
||||
|
||||
// restore noise to html content
|
||||
function restore_noise($text) {
|
||||
while(($pos=strpos($text, '___noise___'))!==false) {
|
||||
$key = '___noise___'.$text[$pos+11].$text[$pos+12].$text[$pos+13];
|
||||
if (isset($this->noise[$key]))
|
||||
$text = substr($text, 0, $pos).$this->noise[$key].substr($text, $pos+14);
|
||||
}
|
||||
return $text;
|
||||
}
|
||||
|
||||
function __toString() {
|
||||
return $this->root->innertext();
|
||||
}
|
||||
|
||||
function __get($name) {
|
||||
switch($name) {
|
||||
case 'outertext': return $this->root->innertext();
|
||||
case 'innertext': return $this->root->innertext();
|
||||
case 'plaintext': return $this->root->text();
|
||||
}
|
||||
}
|
||||
|
||||
// camel naming conventions
|
||||
function childNodes($idx=-1) {return $this->root->childNodes($idx);}
|
||||
function firstChild() {return $this->root->first_child();}
|
||||
function lastChild() {return $this->root->last_child();}
|
||||
function getElementById($id) {return $this->find("#$id", 0);}
|
||||
function getElementsById($id, $idx=null) {return $this->find("#$id", $idx);}
|
||||
function getElementByTagName($name) {return $this->find($name, 0);}
|
||||
function getElementsByTagName($name, $idx=-1) {return $this->find($name, $idx);}
|
||||
function loadFile() {$args = func_get_args();$this->load(call_user_func_array('file_get_contents', $args), true);}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user