Tidy

目录

简介

An HTML node in an HTML file, as detected by tidy.

类摘要

tidy

class tidy {

/* 属性 */

public string$tidy->errorBuffer;

/* 方法 */

public tidyNode body ( void )

public bool cleanRepair ( void )

public __construct ([ string $filename [, mixed $config [, string $encoding [, bool $use_include_path ]]]] )

public bool diagnose ( void )

public array getConfig ( void )

public int getHtmlVer ( void )

public mixed getOpt ( string $option )

public string getOptDoc ( string $optname )

public string getRelease ( void )

public int getStatus ( void )

public tidyNode head ( void )

public tidyNode html ( void )

public bool isXhtml ( void )

public bool isXml ( void )

public bool parseFile ( string $filename [, mixed $config [, string $encoding [, bool $use_include_path = FALSE ]]] )

public bool parseString ( string $input [, mixed $config [, string $encoding ]] )

public string repairFile ( string $filename [, mixed $config [, string $encoding [, bool $use_include_path = FALSE ]]] )

public string repairString ( string $data [, mixed $config [, string $encoding ]] )

public tidyNode root ( void )

}

tidy::body

tidy_get_body

Returns a tidyNode object starting from the <body> tag of the tidy parse tree

说明

面向对象风格

public tidyNode tidy::body ( void )

过程化风格

tidyNode tidy_get_body ( tidy $object )

Returns a tidyNode object starting from the <body> tag of the tidy parse tree.

参数

object
The Tidy 对象。

返回值

Returns a tidyNode object starting from the <body> tag of the tidy parse tree.

范例

示例 #1 tidy::getBody example

<?php
$html = '
<html>
  <head>
    <title>test</title>
  </head>
  <body>
    <p>paragraph</p>
  </body>
</html>';

$tidy = tidy_parse_string($html);

$body = $tidy->Body();
echo $body->value;
?>

以上例程会输出:

<body>
<p>paragraph</p>
</body>

参见

  • tidy::head
  • tidy::html

tidy::cleanRepair

tidy_clean_repair

Execute configured cleanup and repair operations on parsed markup

说明

面向对象风格

public bool tidy::cleanRepair ( void )

过程化风格

bool tidy_clean_repair ( tidy $object )

This function cleans and repairs the given tidy object.

参数

object
The Tidy 对象。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 tidy::cleanrepair example

<?php
$html = '<p>test</I>';

$tidy = tidy_parse_string($html);
$tidy->cleanRepair();

echo $tidy;
?>

以上例程会输出:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title></title>
</head>
<body>
<p>test</p>
</body>
</html>

参见

  • tidy::repairFile
  • tidy::repairString

tidy::__construct

Constructs a new tidy object

说明

public tidy::__construct ([ string $filename [, mixed $config [, string $encoding [, bool $use_include_path ]]]] )

Constructs a new tidy object.

参数

filename
If the filename parameter is given, this function will also read that file and initialize the object with the file, acting like tidy_parse_file.

config
The config config can be passed either as an array or as a string. If a string is passed, it is interpreted as the name of the configuration file, otherwise, it is interpreted as the options themselves.

For an explanation about each option, visit » http://tidy.sourceforge.net/docs/quickref.html.

encoding
The encoding parameter sets the encoding for input/output documents. The possible values for encoding are: ascii, latin0, latin1, raw, utf8, iso2022, mac, win1252, ibm858, utf16, utf16le, utf16be, big5, and shiftjis.

use_include_path
Search for the file in the include_path.

返回值

Returns the new tidy instance.

范例

示例 #1 tidy::__construct example

<?php

$html = <<< HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>title</title></head>
<body>
<p>paragraph <bt />
text</p>
</body></html>

HTML;

$tidy = new tidy();
$tidy->ParseString($html);

$tidy->cleanRepair();

if ($tidy->errorBuffer) {
    echo "The following errors were detected:\n";
    echo $tidy->errorBuffer;
}

?>

以上例程会输出:

The following errors were detected:
line 8 column 14 - Error: <bt> is not recognized!
line 8 column 14 - Warning: discarding unexpected <bt>

参见

  • tidy::parseFile
  • tidy::parseString

tidy::diagnose

tidy_diagnose

Run configured diagnostics on parsed and repaired markup

说明

面向对象风格

public bool tidy::diagnose ( void )

过程化风格

bool tidy_diagnose ( tidy $object )

Runs diagnostic tests on the given tidy object, adding some more information about the document in the error buffer.

参数

object
The Tidy 对象。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 tidy::diagnose example

<?php

$html = <<< HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<p>paragraph</p>
HTML;

$tidy = tidy_parse_string($html);
$tidy->cleanRepair();

// note the difference between the two outputs
echo $tidy->errorBuffer . "\n";

$tidy->diagnose();
echo $tidy->errorBuffer;

?>

以上例程会输出:

line 4 column 1 - Warning: <p> isn't allowed in <head> elements
line 4 column 1 - Warning: inserting missing 'title' element
line 4 column 1 - Warning: <p> isn't allowed in <head> elements
line 4 column 1 - Warning: inserting missing 'title' element
Info: Doctype given is "-//W3C//DTD XHTML 1.0 Strict//EN"
Info: Document content looks like XHTML 1.0 Strict
2 warnings, 0 errors were found!

参见

  • tidy::errorBuffer

tidy::$errorBuffer

tidy_get_error_buffer

Return warnings and errors which occurred parsing the specified document

说明

面向对象风格 (property):

public string$tidy->errorBuffer;

过程化风格:

string tidy_get_error_buffer ( tidy $tidy )

Returns warnings and errors which occurred parsing the specified document.

参数

tidy
The Tidy 对象。

返回值

Returns the error buffer as a string.

范例

示例 #1 tidy_get_error_buffer example

<?php
$html = '<p>paragraph</p>';

$tidy = tidy_parse_string($html);

echo tidy_get_error_buffer($tidy);
/* or in OO: */
echo $tidy->errorBuffer;
?>

以上例程会输出:

line 1 column 1 - Warning: missing <!DOCTYPE> declaration
line 1 column 1 - Warning: inserting missing 'title' element

参见

  • tidy_access_count
  • tidy_error_count
  • tidy_warning_count

tidy::getConfig

tidy_get_config

Get current Tidy configuration

说明

面向对象风格

public array tidy::getConfig ( void )

过程化风格

array tidy_get_config ( tidy $object )

Gets the list of the configuration options in use by the given tidy object.

参数

object
The Tidy 对象。

返回值

Returns an array of configuration options.

For an explanation about each option, visit » http://tidy.sourceforge.net/docs/quickref.html.

范例

示例 #1 tidy::getConfig example

<?php
$html = '<p>test</p>';
$config = array('indent' => TRUE,
                'output-xhtml' => TRUE,
                'wrap' => 200);

$tidy = tidy_parse_string($html, $config);

print_r($tidy->getConfig());
?>

以上例程会输出:

Array
(
    [indent-spaces] => 2
    [wrap] => 200
    [tab-size] => 8
    [char-encoding] => 1
    [input-encoding] => 3
    [output-encoding] => 1
    [newline] => 1
    [doctype-mode] => 1
    [doctype] =>
    [repeated-attributes] => 1
    [alt-text] =>
    [slide-style] =>
    [error-file] =>
    [output-file] =>
    [write-back] =>
    [markup] => 1
    [show-warnings] => 1
    [quiet] =>
    [indent] => 1
    [hide-endtags] =>
    [input-xml] =>
    [output-xml] => 1
    [output-xhtml] => 1
    [output-html] =>
    [add-xml-decl] =>
    [uppercase-tags] =>
    [uppercase-attributes] =>
    [bare] =>
    [clean] =>
    [logical-emphasis] =>
    [drop-proprietary-attributes] =>
    [drop-font-tags] =>
    [drop-empty-paras] => 1
    [fix-bad-comments] => 1
    [break-before-br] =>
    [split] =>
    [numeric-entities] =>
    [quote-marks] =>
    [quote-nbsp] => 1
    [quote-ampersand] => 1
    [wrap-attributes] =>
    [wrap-script-literals] =>
    [wrap-sections] => 1
    [wrap-asp] => 1
    [wrap-jste] => 1
    [wrap-php] => 1
    [fix-backslash] => 1
    [indent-attributes] =>
    [assume-xml-procins] =>
    [add-xml-space] =>
    [enclose-text] =>
    [enclose-block-text] =>
    [keep-time] =>
    [word-2000] =>
    [tidy-mark] =>
    [gnu-emacs] =>
    [gnu-emacs-file] =>
    [literal-attributes] =>
    [show-body-only] =>
    [fix-uri] => 1
    [lower-literals] => 1
    [hide-comments] =>
    [indent-cdata] =>
    [force-output] => 1
    [show-errors] => 6
    [ascii-chars] => 1
    [join-classes] =>
    [join-styles] => 1
    [escape-cdata] =>
    [language] =>
    [ncr] => 1
    [output-bom] => 2
    [replace-color] =>
    [css-prefix] =>
    [new-inline-tags] =>
    [new-blocklevel-tags] =>
    [new-empty-tags] =>
    [new-pre-tags] =>
    [accessibility-check] => 0
    [vertical-space] =>
    [punctuation-wrap] =>
    [merge-divs] => 1
)

参见

  • tidy_reset_config
  • tidy_save_config

tidy::getHtmlVer

tidy_get_html_ver

Get the Detected HTML version for the specified document

说明

面向对象风格

public int tidy::getHtmlVer ( void )

过程化风格

int tidy_get_html_ver ( tidy $object )

Returns the detected HTML version for the specified tidy object.

参数

object
The Tidy 对象。

返回值

Returns the detected HTML version.

Warning

This function is not yet implemented in the Tidylib itself, so it always return 0.

tidy::getOpt

tidy_getopt

Returns the value of the specified configuration option for the tidy document

说明

面向对象风格

public mixed tidy::getOpt ( string $option )

过程化风格

mixed tidy_getopt ( tidy $object , string $option )

Returns the value of the specified option for the specified tidy object.

参数

object
The Tidy 对象。

option
You will find a list with each configuration option and their types at: » http://tidy.sourceforge.net/docs/quickref.html.

返回值

Returns the value of the specified option. The return type depends on the type of the specified one.

范例

示例 #1 tidy_getopt example

<?php

$html ='<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Title</title></head>
<body>

<p><img src="img.png"></p>

</body></html>';

$config = array('accessibility-check' => 3,
                'alt-text' => 'some text');

$tidy = new tidy();
$tidy->parseString($html, $config);


var_dump($tidy->getOpt('accessibility-check')); //integer
var_dump($tidy->getOpt('lower-literals')); //boolean
var_dump($tidy->getOpt('alt-text')); //string

?>

以上例程会输出:

int(3)
bool(true)
string(9) "some text"

tidy::getOptDoc

tidy_get_opt_doc

Returns the documentation for the given option name

说明

面向对象风格

public string tidy::getOptDoc ( string $optname )

过程化风格

string tidy_get_opt_doc ( tidy $object , string $optname )

tidy_get_opt_doc returns the documentation for the given option name.

Note:

You need at least libtidy from 25 April, 2005 for this function be available.

参数

object
The Tidy 对象。

optname
The option name

返回值

Returns a string if the option exists and has documentation available, or FALSE otherwise.

范例

示例 #1 Print all options along with their documentation and default value

<?php

$tidy = new tidy;
$config = $tidy->getConfig();

ksort($config);

foreach ($config as $opt => $val) {

    if (!$doc = $tidy->getOptDoc($opt))
        $doc = 'no documentation available!';

    $val = ($tidy->getOpt($opt) === true)  ? 'true'  : $val;
    $val = ($tidy->getOpt($opt) === false) ? 'false' : $val;

    echo "<p><b>$opt</b> (default: '$val')<br />".
         "$doc</p><hr />\n";
}

?>

参见

  • tidy::getconfig
  • tidy::getopt

tidy::getRelease

tidy_get_release

Get release date (version) for Tidy library

说明

面向对象风格

public string tidy::getRelease ( void )

过程化风格

string tidy_get_release ( void )

Gets the release date of the Tidy library.

返回值

Returns a string with the release date of the Tidy library, which may be 'unknown'.

tidy::getStatus

tidy_get_status

Get status of specified document

说明

面向对象风格

public int tidy::getStatus ( void )

过程化风格

int tidy_get_status ( tidy $object )

Returns the status for the specified tidy object.

参数

object
The Tidy 对象。

返回值

Returns 0 if no error/warning was raised, 1 for warnings or accessibility errors, or 2 for errors.

范例

示例 #1 tidy::getStatus example

<?php
$html = '<p>paragraph</i>';
$tidy = new tidy();
$tidy->parseString($html);

$tidy2 = new tidy();
$html2 = '<bogus>test</bogus>';
$tidy2->parseString($html2);

echo $tidy->getStatus(); //1

echo $tidy2->getStatus(); //2
?>

tidy::head

tidy_get_head

Returns a tidyNode object starting from the <head> tag of the tidy parse tree

说明

面向对象风格

public tidyNode tidy::head ( void )

过程化风格

tidyNode tidy_get_head ( tidy $object )

Returns a tidyNode object starting from the <head> tag of the tidy parse tree.

参数

object
The Tidy 对象。

返回值

Returns the tidyNode object.

范例

示例 #1 tidy::head example

<?php
$html = '
<html>
  <head>
    <title>test</title>
  </head>
  <body>
    <p>paragraph</p>
  </body>
</html>';

$tidy = tidy_parse_string($html);

$head = $tidy->head();
echo $head->value;
?>

以上例程会输出:

<head>
<title>test</title>
</head>

参见

  • tidy::body
  • tidy::html

tidy::html

tidy_get_html

Returns a tidyNode object starting from the <html> tag of the tidy parse tree

说明

面向对象风格

public tidyNode tidy::html ( void )

过程化风格

tidyNode tidy_get_html ( tidy $object )

Returns a tidyNode object starting from the <html> tag of the tidy parse tree.

参数

object
The Tidy 对象。

返回值

Returns the tidyNode object.

范例

示例 #1 tidy::html example

<?php
$html = '
<html>
  <head>
    <title>test</title>
  </head>
  <body>
    <p>paragraph</p>
  </body>
</html>';

$tidy = tidy_parse_string($html);

$html = $tidy->html();
echo $html->value;
?>

以上例程会输出:

<html>
<head>
<title>test</title>
</head>
<body>
<p>paragraph</p>
</body>
</html>

参见

  • tidy::body
  • tidy::head

tidy::isXhtml

tidy_is_xhtml

Indicates if the document is a XHTML document

说明

面向对象风格

public bool tidy::isXhtml ( void )

过程化风格

bool tidy_is_xhtml ( tidy $object )

Tells if the document is a XHTML document.

参数

object
The Tidy 对象。

返回值

This function returns TRUE if the specified tidy object is a XHTML document, or FALSE otherwise.

Warning

This function is not yet implemented in the Tidylib itself, so it always return FALSE.

tidy::isXml

tidy_is_xml

Indicates if the document is a generic (non HTML/XHTML) XML document

说明

面向对象风格

public bool tidy::isXml ( void )

过程化风格

bool tidy_is_xml ( tidy $object )

Tells if the document is a generic (non HTML/XHTML) XML document.

参数

object
The Tidy 对象。

返回值

This function returns TRUE if the specified tidy object is a generic XML document (non HTML/XHTML), or FALSE otherwise.

Warning

This function is not yet implemented in the Tidylib itself, so it always return FALSE.

tidy::parseFile

tidy_parse_file

Parse markup in file or URI

说明

面向对象风格

public bool tidy::parseFile ( string $filename [, mixed $config [, string $encoding [, bool $use_include_path = FALSE ]]] )

过程化风格

tidy tidy_parse_file ( string $filename [, mixed $config [, string $encoding [, bool $use_include_path = FALSE ]]] )

Parses the given file.

参数

filename
If the filename parameter is given, this function will also read that file and initialize the object with the file, acting like tidy_parse_file.

config
The config config can be passed either as an array or as a string. If a string is passed, it is interpreted as the name of the configuration file, otherwise, it is interpreted as the options themselves.

For an explanation about each option, see » http://tidy.sourceforge.net/docs/quickref.html.

encoding
The encoding parameter sets the encoding for input/output documents. The possible values for encoding are: ascii, latin0, latin1, raw, utf8, iso2022, mac, win1252, ibm858, utf16, utf16le, utf16be, big5, and shiftjis.

use_include_path
Search for the file in the include_path.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 tidy::parseFile example

<?php
$tidy = new tidy();
$tidy->parseFile('file.html');

$tidy->cleanRepair();

if(!empty($tidy->errorBuffer)) {
    echo "The following errors or warnings occurred:\n";
    echo $tidy->errorBuffer;
}
?>

参见

  • tidy::parsestring
  • tidy::repairfile
  • tidy::repairstring

tidy::parseString

tidy_parse_string

Parse a document stored in a string

说明

面向对象风格

public bool tidy::parseString ( string $input [, mixed $config [, string $encoding ]] )

过程化风格

tidy tidy_parse_string ( string $input [, mixed $config [, string $encoding ]] )

Parses a document stored in a string.

参数

input
The data to be parsed.

config
The config config can be passed either as an array or as a string. If a string is passed, it is interpreted as the name of the configuration file, otherwise, it is interpreted as the options themselves.

For an explanation about each option, visit » http://tidy.sourceforge.net/docs/quickref.html.

encoding
The encoding parameter sets the encoding for input/output documents. The possible values for encoding are: ascii, latin0, latin1, raw, utf8, iso2022, mac, win1252, ibm858, utf16, utf16le, utf16be, big5, and shiftjis.

返回值

Returns a new tidy instance.

范例

示例 #1 tidy::parseString example

<?php
ob_start();
?>

<html>
  <head>
   <title>test</title>
  </head>
  <body>
   <p>error<br>another line</i>
  </body>
</html>

<?php

$buffer = ob_get_clean();
$config = array('indent' => TRUE,
                'output-xhtml' => TRUE,
                'wrap' => 200);

$tidy = tidy_parse_string($buffer, $config, 'UTF8');

$tidy->cleanRepair();
echo $tidy;
?>

以上例程会输出:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>
      test
    </title>
  </head>
  <body>
    <p>
      error<br />
      another line
    </p>
  </body>
</html>

参见

  • tidy::parseFile
  • tidy::repairFile
  • tidy::repairString

tidy::repairFile

tidy_repair_file

Repair a file and return it as a string

说明

面向对象风格

public string tidy::repairFile ( string $filename [, mixed $config [, string $encoding [, bool $use_include_path = FALSE ]]] )

过程化风格

string tidy_repair_file ( string $filename [, mixed $config [, string $encoding [, bool $use_include_path = FALSE ]]] )

Repairs the given file and returns it as a string.

参数

filename
The file to be repaired.

config
The config config can be passed either as an array or as a string. If a string is passed, it is interpreted as the name of the configuration file, otherwise, it is interpreted as the options themselves.

Check http://tidy.sourceforge.net/docs/quickref.html for an explanation about each option.

encoding
The encoding parameter sets the encoding for input/output documents. The possible values for encoding are: ascii, latin0, latin1, raw, utf8, iso2022, mac, win1252, ibm858, utf16, utf16le, utf16be, big5, and shiftjis.

use_include_path
Search for the file in the include_path.

返回值

Returns the repaired contents as a string.

范例

示例 #1 tidy::repairFile example

<?php
$file = 'file.html';

$tidy = new tidy();
$repaired = $tidy->repairfile($file);
rename($file, $file . '.bak');

file_put_contents($file, $repaired);
?>

参见

  • tidy::parseFile
  • tidy::parseString
  • tidy::repairString

tidy::repairString

tidy_repair_string

Repair a string using an optionally provided configuration file

说明

面向对象风格

public string tidy::repairString ( string $data [, mixed $config [, string $encoding ]] )

过程化风格

string tidy_repair_string ( string $data [, mixed $config [, string $encoding ]] )

Repairs the given string.

参数

data
The data to be repaired.

config
The config config can be passed either as an array or as a string. If a string is passed, it is interpreted as the name of the configuration file, otherwise, it is interpreted as the options themselves.

Check » http://tidy.sourceforge.net/docs/quickref.html for an explanation about each option.

encoding
The encoding parameter sets the encoding for input/output documents. The possible values for encoding are: ascii, latin0, latin1, raw, utf8, iso2022, mac, win1252, ibm858, utf16, utf16le, utf16be, big5, and shiftjis.

返回值

Returns the repaired string.

范例

示例 #1 tidy::repairString example

<?php
ob_start();
?>

<html>
  <head>
    <title>test</title>
  </head>
  <body>
    <p>error</i>
  </body>
</html>

<?php

$buffer = ob_get_clean();
$tidy = new tidy();
$clean = $tidy->repairString($buffer);

echo $clean;
?>

以上例程会输出:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>test</title>
</head>
<body>
<p>error</p>
</body>
</html>

参见

  • tidy::parseFile
  • tidy::parseString
  • tidy::repairFile

tidy::root

tidy_get_root

Returns a tidyNode object representing the root of the tidy parse tree

说明

面向对象风格

public tidyNode tidy::root ( void )

过程化风格

tidyNode tidy_get_root ( tidy $object )

Returns a tidyNode object representing the root of the tidy parse tree.

参数

object
The Tidy 对象。

返回值

Returns the tidyNode object.

范例

示例 #1 tidy::root example

<?php

$html = <<< HTML
<html><body>

<p>paragraph</p>
<br/>

</body></html>
HTML;

$tidy = tidy_parse_string($html);
dump_nodes($tidy->root(), 1);


function dump_nodes($node, $indent) {

    if($node->hasChildren()) {
        foreach($node->child as $child) {
            echo str_repeat('.', $indent*2) . ($child->name ? $child->name : '"'.$child->value.'"'). "\n";

            dump_nodes($child, $indent+1);
        }
    }
}
?>

以上例程会输出:

..html
....head
......title
....body
......p
........"paragraph"
......br

简介

An HTML node in an HTML file, as detected by tidy.

类摘要

final tidyNode

class tidyNode {

/* 属性 */

public string$value;

public string$name;

public int$type;

public int$line;

public int$column;

public bool$proprietary;

public int$id;

public array$attribute;

public array$child;

/* 方法 */

private __construct ( void )

public tidyNode getParent ( void )

public bool hasChildren ( void )

public bool hasSiblings ( void )

public bool isAsp ( void )

public bool isComment ( void )

public bool isHtml ( void )

public bool isJste ( void )

public bool isPhp ( void )

public bool isText ( void )

}

属性

value
The HTML representation of the node, including the surrounding tags.

name
The name of the HTML node

type
The type of the node (one of the nodetype constants, e.g. TIDY_NODETYPE_PHP)

line
The line number at which the tags is located in the file

column
The column number at which the tags is located in the file

proprietary
Indicates if the node is a proprietary tag

id
The ID of the node (one of the tag constants, e.g. TIDY_TAG_FRAME)

attribute
An array of string, representing the attributes names (as keys) of the current node.

child
An array of tidyNode, representing the children of the current node.

版本说明
5.1.0line, column and proprietary were added

tidyNode::__construct

Private constructor to disallow direct instantiation

说明

private tidyNode::__construct ( void )

参数

此函数没有参数。

tidyNode::getParent

Returns the parent node of the current node

说明

public tidyNode tidyNode::getParent ( void )

Returns the parent node of the current node.

范例

示例 #1 tidyNode::hasChildren example

<?php

$html = <<< HTML
<html><head>
<?php echo '<title>title</title>'; ?>
<# 
  /* JSTE code */
  alert('Hello World'); 
#>
 </head>
 <body>
 Hello World
 </body>
</html>

HTML;


$tidy = tidy_parse_string($html);
$num = 0;

$node = $tidy->html()->child[0]->child[0];

var_dump($node->getparent()->name);
?>

以上例程会输出:

string(4) "head"

返回值

Returns a tidyNode if the node has a parent, or NULL otherwise.

tidyNode::hasChildren

Checks if a node has children

说明

public bool tidyNode::hasChildren ( void )

Tells if the node has children.

返回值

Returns TRUE if the node has children, FALSE otherwise.

范例

示例 #1 tidyNode::hasChildren example

<?php

$html = <<< HTML
<html><head>
<?php echo '<title>title</title>'; ?>
<# 
  /* JSTE code */
  alert('Hello World'); 
#>
</head>
<body>

<?php
  // PHP code
  echo 'hello world!';
?>

<%
  /* ASP code */
  response.write("Hello World!")
%>

<!-- Comments -->
Hello World
</body></html>
Outside HTML
HTML;


$tidy = tidy_parse_string($html);
$num = 0;

// the head tag
var_dump($tidy->html()->child[0]->hasChildren());

// the php inside the head tag
var_dump($tidy->html()->child[0]->child[0]->hasChildren());

?>

以上例程会输出:

bool(true)
bool(false)

tidyNode::hasSiblings

Checks if a node has siblings

说明

public bool tidyNode::hasSiblings ( void )

Tells if the node has siblings.

返回值

Returns TRUE if the node has siblings, FALSE otherwise.

范例

示例 #1 tidyNode::hasSiblings example

<?php

$html = <<< HTML
<html><head>
<?php echo '<title>title</title>'; ?>
<# 
  /* JSTE code */
  alert('Hello World'); 
#>
</head>
<body>

<?php
  // PHP code
  echo 'hello world!';
?>

<%
  /* ASP code */
  response.write("Hello World!")
%>

<!-- Comments -->
Hello World
</body></html>
Outside HTML
HTML;


$tidy = tidy_parse_string($html);
$num = 0;

// the html tag
var_dump($tidy->html()->hasSiblings());

// the head tag
var_dump($tidy->html()->child[0]->hasSiblings());

?>

以上例程会输出:

bool(false)
bool(true)

tidyNode::isAsp

Checks if this node is ASP

说明

public bool tidyNode::isAsp ( void )

Tells whether the current node is ASP.

返回值

Returns TRUE if the node is ASP, FALSE otherwise.

范例

示例 #1 Extract ASP code from a mixed HTML document

<?php

$html = <<< HTML
<html><head>
<?php echo '<title>title</title>'; ?>
<# 
  /* JSTE code */
  alert('Hello World'); 
#>
</head>
<body>

<?php
  // PHP code
  echo 'hello world!';
?>

<%
  /* ASP code */
  response.write("Hello World!")
%>

<!-- Comments -->
Hello World
</body></html>
Outside HTML
HTML;


$tidy = tidy_parse_string($html);
$num = 0;

get_nodes($tidy->html());

function get_nodes($node) {

    // check if the current node is of requested type
    if($node->isAsp()) {
        echo "\n\n# asp node #" . ++$GLOBALS['num'] . "\n";
        echo $node->value;
    }

    // check if the current node has childrens
    if($node->hasChildren()) {
        foreach($node->child as $child) {
            get_nodes($child);
        }
    }
}

?>

以上例程会输出:

# asp node #1
<%
  /* ASP code */
  response.write("Hello World!")
%>

tidyNode::isComment

Checks if a node represents a comment

说明

public bool tidyNode::isComment ( void )

Tells if the node is a comment.

返回值

Returns TRUE if the node is a comment, FALSE otherwise.

范例

示例 #1 Extract comments from a mixed HTML document

<?php

$html = <<< HTML
<html><head>
<?php echo '<title>title</title>'; ?>
<# 
  /* JSTE code */
  alert('Hello World'); 
#>
</head>
<body>

<?php
  // PHP code
  echo 'hello world!';
?>

<%
  /* ASP code */
  response.write("Hello World!")
%>

<!-- Comments -->
Hello World
</body></html>
Outside HTML
HTML;


$tidy = tidy_parse_string($html);
$num = 0;

get_nodes($tidy->html());

function get_nodes($node) {

    // check if the current node is of requested type
    if($node->isComment()) {
        echo "\n\n# comment node #" . ++$GLOBALS['num'] . "\n";
        echo $node->value;
    }

    // check if the current node has childrens
    if($node->hasChildren()) {
        foreach($node->child as $child) {
            get_nodes($child);
        }
    }
}

?>

以上例程会输出:

# comment node #1
<!-- Comments -->

tidyNode::isHtml

Checks if a node is part of a HTML document

说明

public bool tidyNode::isHtml ( void )

Tells if the node is part of HTML document.

返回值

Returns TRUE if the node is part of a HTML document, FALSE otherwise.

范例

示例 #1 Extract HTML code from a mixed HTML document

<?php

$html = <<< HTML
<html><head>
<?php echo '<title>title</title>'; ?>
<# 
  /* JSTE code */
  alert('Hello World'); 
#>
</head>
<body>

<?php
  // PHP code
  echo 'hello world!';
?>

<%
  /* ASP code */
  response.write("Hello World!")
%>

<!-- Comments -->
Hello World
</body></html>
Outside HTML
HTML;


$tidy = tidy_parse_string($html);
$num = 0;

get_nodes($tidy->html());

function get_nodes($node) {
    // check if the current node is of requested type
    if($node->isHtml()) {
        echo "\n\n# html node #" . ++$GLOBALS['num'] . "\n";
        echo $node->value;
    }

    // check if the current node has childrens
    if($node->hasChildren()) {
        foreach($node->child as $child) {
            get_nodes($child);
        }
    }
}

?>

以上例程会输出:

# html node #1
<html>
<head>
<?php echo '<title>title</title>'; ?><# 
  /* JSTE code */
  alert('Hello World'); 
#>
<title></title>
</head>
<body>
<?php
  // PHP code
  echo 'hello world!';
?><%
  /* ASP code */
  response.write("Hello World!")
%><!-- Comments -->
Hello WorldOutside HTML
</body>
</html>

# html node #2
<head>
<?php echo '<title>title</title>'; ?><# 
  /* JSTE code */
  alert('Hello World'); 
#>
<title></title>
</head>


# html node #3
<?php echo '<title>title</title>'; ?>

# html node #4
<# 
  /* JSTE code */
  alert('Hello World'); 
#>

# html node #5
<title></title>


# html node #6
<body>
<?php
  // PHP code
  echo 'hello world!';
?><%
  /* ASP code */
  response.write("Hello World!")
%><!-- Comments -->
Hello WorldOutside HTML
</body>


# html node #7
<?php
  // PHP code
  echo 'hello world!';
?>

# html node #8
<%
  /* ASP code */
  response.write("Hello World!")
%>

# html node #9
<!-- Comments -->


# html node #10
Hello World

# html node #11
Outside HTML

tidyNode::isJste

Checks if this node is JSTE

说明

public bool tidyNode::isJste ( void )

Tells if the node is JSTE.

返回值

Returns TRUE if the node is JSTE, FALSE otherwise.

范例

示例 #1 Extract JSTE code from a mixed HTML document

<?php

$html = <<< HTML
<html><head>
<?php echo '<title>title</title>'; ?>
<# 
  /* JSTE code */
  alert('Hello World'); 
#>
</head>
<body>

<?php
  // PHP code
  echo 'hello world!';
?>

<%
  /* ASP code */
  response.write("Hello World!")
%>

<!-- Comments -->
Hello World
</body></html>
Outside HTML
HTML;


$tidy = tidy_parse_string($html);
$num = 0;

get_nodes($tidy->html());

function get_nodes($node) {

    // check if the current node is of requested type
    if($node->isJste()) {
        echo "\n\n# jste node #" . ++$GLOBALS['num'] . "\n";
        echo $node->value;
    }

    // check if the current node has childrens
    if($node->hasChildren()) {
        foreach($node->child as $child) {
            get_nodes($child);
        }
    }
}

?>

以上例程会输出:

# jste node #1
<# 
  /* JSTE code */
  alert('Hello World'); 
#>

tidyNode::isPhp

Checks if a node is PHP

说明

public bool tidyNode::isPhp ( void )

Tells if the node is PHP.

返回值

Returns TRUE if the current node is PHP code, FALSE otherwise.

范例

示例 #1 Extract PHP code from a mixed HTML document

<?php

$html = <<< HTML
<html><head>
<?php echo '<title>title</title>'; ?>
<# 
  /* JSTE code */
  alert('Hello World'); 
#>
</head>
<body>

<?php
  // PHP code
  echo 'hello world!';
?>

<%
  /* ASP code */
  response.write("Hello World!")
%>

<!-- Comments -->
Hello World
</body></html>
Outside HTML
HTML;


$tidy = tidy_parse_string($html);
$num = 0;

get_nodes($tidy->html());

function get_nodes($node) {

    // check if the current node is of requested type
    if($node->isPhp()) {
        echo "\n\n# php node #" . ++$GLOBALS['num'] . "\n";
        echo $node->value;
    }

    // check if the current node has childrens
    if($node->hasChildren()) {
        foreach($node->child as $child) {
            get_nodes($child);
        }
    }
}

?>

以上例程会输出:

# php node #1
<?php echo '<title>title</title>'; ?>

# php node #2
<?php
  // PHP code
  echo 'hello world!';
?>

tidyNode::isText

Checks if a node represents text (no markup)

说明

public bool tidyNode::isText ( void )

Tells if the node represents a text (without any markup).

返回值

Returns TRUE if the node represent a text, FALSE otherwise.

范例

示例 #1 Extract text from a mixed HTML document

<?php

$html = <<< HTML
<html><head>
<?php echo '<title>title</title>'; ?>
<# 
  /* JSTE code */
  alert('Hello World'); 
#>
</head>
<body>

<?php
  // PHP code
  echo 'hello world!';
?>

<%
  /* ASP code */
  response.write("Hello World!")
%>

<!-- Comments -->
Hello World
</body></html>
Outside HTML
HTML;


$tidy = tidy_parse_string($html);
$num = 0;

get_nodes($tidy->html());

function get_nodes($node) {

    // check if the current node is of requested type
    if($node->isText()) {
        echo "\n\n# text node #" . ++$GLOBALS['num'] . "\n";
        echo $node->value;
    }

    // check if the current node has childrens
    if($node->hasChildren()) {
        foreach($node->child as $child) {
            get_nodes($child);
        }
    }
}

?>

以上例程会输出:

# text node #1
Hello World

# text node #2
Outside HTML