Book/tidy-Phpdoc专题

Tidy

目录

简介

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

类摘要

tidy

class tidy {

/* 属性 */

public <span class="type">string$tidy->errorBuffer;

/* 方法 */

public <span class="methodname">__construct ([ <span class="methodparam"><span class="type">stringnull $filename = null [, <span class="type">arraystring<span class="type">null $config = null [, <span class="type">string<span class="type">null $encoding = null [, <span class="type">bool $useIncludePath = false ]]]] )

public <span class="type">tidyNodenull <span class="methodname">body ( void )

public bool cleanRepair ( <span class="methodparam">void )

public bool diagnose ( <span class="methodparam">void )

string<span class="type">false <span class="methodname">tidy_get_error_buffer ( <span class="methodparam">tidy $tidy )

public array getConfig ( <span class="methodparam">void )

public int <span class="methodname">getHtmlVer ( <span class="methodparam">void )

public <span class="type">stringint<span class="type">bool getOpt ( string $option )

public <span class="type">stringfalse <span class="methodname">getOptDoc ( <span class="type">string $option )

public string getRelease ( <span class="methodparam">void )

public int <span class="methodname">getStatus ( <span class="methodparam">void )

public <span class="type">tidyNodenull <span class="methodname">head ( void )

public <span class="type">tidyNodenull <span class="methodname">html ( void )

public bool isXhtml ( <span class="methodparam">void )

public bool isXml ( <span class="methodparam">void )

public bool parseFile ( <span class="methodparam">string $filename [, <span class="type">arraystring<span class="type">null $config = null [, <span class="type">string<span class="type">null $encoding = null [, <span class="type">bool $useIncludePath = false ]]] )

public bool parseString ( <span class="methodparam">string $string [, <span class="type">arraystring<span class="type">null $config = null [, <span class="type">string<span class="type">null $encoding = null ]] )

public <span class="modifier">static <span class="type">stringfalse <span class="methodname">repairFile ( <span class="type">string $filename [, <span class="methodparam"><span class="type">arraystring<span class="type">null $config = null [, <span class="type">string<span class="type">null $encoding = null [, <span class="type">bool $useIncludePath = false ]]] )

public <span class="modifier">static <span class="type">stringfalse <span class="methodname">repairString ( <span class="type">string $string [, <span class="methodparam"><span class="type">arraystring<span class="type">null $config = null [, <span class="type">string<span class="type">null $encoding = null ]] )

public <span class="type">tidyNodenull <span class="methodname">root ( void )

}

tidy::body

tidy_get_body

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

说明

面向对象风格

public <span class="type">tidyNodenull <span class="methodname">tidy::body ( <span class="methodparam">void )

过程化风格

tidyNode<span class="type">null <span class="methodname">tidy_get_body ( <span class="methodparam">tidy $tidy )

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

参数

tidy
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 ( <span class="methodparam">void )

过程化风格

bool <span class="methodname">tidy_clean_repair ( <span class="methodparam">tidy $tidy )

This function cleans and repairs the given tidy tidy.

参数

tidy
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 <span class="methodname">tidy::__construct ([ <span class="methodparam"><span class="type">stringnull $filename = null [, <span class="type">arraystring<span class="type">null $config = null [, <span class="type">string<span class="type">null $encoding = null [, <span class="type">bool $useIncludePath = false ]]]] )

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 <span class="function">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://api.html-tidy.org/#quick-reference.

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.

useIncludePath
Search for the file in the include_path.

返回值

Returns the new tidy instance.

更新日志

版本 说明
8.0.0 filename, config, encoding and useIncludePath are nullable now.

范例

示例 #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 ( <span class="methodparam">void )

过程化风格

bool <span class="methodname">tidy_diagnose ( <span class="methodparam">tidy $tidy )

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

参数

tidy
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 <span class="type">string$tidy->errorBuffer;

过程化风格:

string<span class="type">false <span class="methodname">tidy_get_error_buffer ( <span class="methodparam">tidy $tidy )

Returns warnings and errors which occurred parsing the specified document.

参数

tidy
The Tidy 对象。

返回值

Returns the error buffer as a string, or false if the buffer is empty.

范例

示例 #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 ( <span class="methodparam">void )

过程化风格

array <span class="methodname">tidy_get_config ( <span class="methodparam">tidy $tidy )

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

参数

tidy
The Tidy 对象。

返回值

Returns an array of configuration options.

For an explanation about each option, visit » http://api.html-tidy.org/#quick-reference.

范例

示例 #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 <span class="methodname">tidy::getHtmlVer ( <span class="methodparam">void )

过程化风格

int <span class="methodname">tidy_get_html_ver ( <span class="methodparam">tidy $tidy )

Returns the detected HTML version for the specified tidy tidy.

参数

tidy
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 <span class="type">stringint<span class="type">bool <span class="methodname">tidy::getOpt ( <span class="type">string $option )

过程化风格

string<span class="type">intbool <span class="methodname">tidy_getopt ( <span class="type">tidy $tidy , <span class="methodparam">string $option )

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

参数

tidy
The Tidy 对象。

option
You will find a list with each configuration option and their types at: » http://api.html-tidy.org/#quick-reference.

返回值

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 <span class="type">stringfalse <span class="methodname">tidy::getOptDoc ( <span class="methodparam">string $option )

过程化风格

string<span class="type">false <span class="methodname">tidy_get_opt_doc ( <span class="methodparam">tidy $tidy , string $option )

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.

参数

tidy
The Tidy 对象。

option
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 ( <span class="methodparam">void )

过程化风格

string <span class="methodname">tidy_get_release ( <span class="methodparam">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 <span class="methodname">tidy::getStatus ( <span class="methodparam">void )

过程化风格

int <span class="methodname">tidy_get_status ( <span class="methodparam">tidy $tidy )

Returns the status for the specified tidy tidy.

参数

tidy
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 <span class="type">tidyNodenull <span class="methodname">tidy::head ( <span class="methodparam">void )

过程化风格

tidyNode<span class="type">null <span class="methodname">tidy_get_head ( <span class="methodparam">tidy $tidy )

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

参数

tidy
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 <span class="type">tidyNodenull <span class="methodname">tidy::html ( <span class="methodparam">void )

过程化风格

tidyNode<span class="type">null <span class="methodname">tidy_get_html ( <span class="methodparam">tidy $tidy )

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

参数

tidy
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 ( <span class="methodparam">void )

过程化风格

bool <span class="methodname">tidy_is_xhtml ( <span class="methodparam">tidy $tidy )

Tells if the document is a XHTML document.

参数

tidy
The Tidy 对象。

返回值

This function returns true if the specified tidy tidy 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 ( <span class="methodparam">void )

过程化风格

bool <span class="methodname">tidy_is_xml ( <span class="methodparam">tidy $tidy )

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

参数

tidy
The Tidy 对象。

返回值

This function returns true if the specified tidy tidy 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 ( <span class="methodparam">string $filename [, <span class="type">arraystring<span class="type">null $config = null [, <span class="type">string<span class="type">null $encoding = null [, <span class="type">bool $useIncludePath = false ]]] )

过程化风格

tidy<span class="type">false <span class="methodname">tidy_parse_file ( <span class="methodparam">string $filename [, <span class="type">arraystring<span class="type">null $config = null [, <span class="type">string<span class="type">null $encoding = null [, <span class="type">bool $useIncludePath = 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 <span class="function">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://api.html-tidy.org/#quick-reference.

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.

useIncludePath
Search for the file in the include_path.

返回值

tidy::parseFile returns true on success. tidy_parse_file returns a new tidy instance on success. Both, the method and the function return false on failure.

更新日志

版本 说明
8.0.0 config and encoding are nullable now.

范例

示例 #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 ( <span class="methodparam">string $string [, <span class="type">arraystring<span class="type">null $config = null [, <span class="type">string<span class="type">null $encoding = null ]] )

过程化风格

tidy<span class="type">false <span class="methodname">tidy_parse_string ( <span class="methodparam">string $string [, <span class="type">arraystring<span class="type">null $config = null [, <span class="type">string<span class="type">null $encoding = null ]] )

Parses a document stored in a string.

参数

string
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://api.html-tidy.org/#quick-reference.

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.

返回值

tidy::parseString returns true on success. tidy_parse_string returns a new tidy instance on success. Both, the method and the function return false on failure.

更新日志

版本 说明
8.0.0 config and encoding are nullable now.

范例

示例 #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 <span class="modifier">static <span class="type">stringfalse <span class="methodname">tidy::repairFile ( <span class="methodparam">string $filename [, <span class="type">arraystring<span class="type">null $config = null [, <span class="type">string<span class="type">null $encoding = null [, <span class="type">bool $useIncludePath = false ]]] )

过程化风格

string<span class="type">false <span class="methodname">tidy_repair_file ( <span class="methodparam">string $filename [, <span class="type">arraystring<span class="type">null $config = null [, <span class="type">string<span class="type">null $encoding = null [, <span class="type">bool $useIncludePath = 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.

useIncludePath
Search for the file in the include_path.

返回值

Returns the repaired contents as a string, 或者在失败时返回 false.

更新日志

版本 说明
8.0.0 tidy::repairFile is a static method now.
8.0.0 config and encoding are nullable now.

范例

示例 #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 <span class="modifier">static <span class="type">stringfalse <span class="methodname">tidy::repairString ( <span class="methodparam">string $string [, <span class="type">arraystring<span class="type">null $config = null [, <span class="type">string<span class="type">null $encoding = null ]] )

过程化风格

string<span class="type">false <span class="methodname">tidy_repair_string ( <span class="methodparam">string $string [, <span class="type">arraystring<span class="type">null $config = null [, <span class="type">string<span class="type">null $encoding = null ]] )

Repairs the given string.

参数

string
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://api.html-tidy.org/#quick-reference 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, 或者在失败时返回 false.

更新日志

版本 说明
8.0.0 tidy::repairString is a static method now.
8.0.0 config and encoding are nullable now.
8.0.0 This function no longer accepts the useIncludePath parameter.

范例

示例 #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 <span class="type">tidyNodenull <span class="methodname">tidy::root ( <span class="methodparam">void )

过程化风格

tidyNode<span class="type">null <span class="methodname">tidy_get_root ( <span class="methodparam">tidy $tidy )

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

参数

tidy
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 <span class="type">string$value;

public <span class="type">string$name;

public <span class="type">int$type;

public <span class="type">int$line;

public <span class="type">int$column;

public <span class="type">bool$proprietary;

public int$id;

public <span class="type">array$attribute;

public <span class="type">array$child;

/* 方法 */

private <span class="methodname">__construct ( <span class="methodparam">void )

public <span class="type">tidyNodenull <span class="methodname">getParent ( <span class="methodparam">void )

public bool hasChildren ( <span class="methodparam">void )

public bool hasSiblings ( <span class="methodparam">void )

public bool isAsp ( <span class="methodparam">void )

public bool isComment ( <span class="methodparam">void )

public bool isHtml ( <span class="methodparam">void )

public bool isJste ( <span class="methodparam">void )

public bool isPhp ( <span class="methodparam">void )

public bool isText ( <span class="methodparam">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.0 line, column and proprietary were added

tidyNode::__construct

Private constructor to disallow direct instantiation

说明

private <span class="methodname">tidyNode::__construct ( <span class="methodparam">void )

参数

此函数没有参数。

tidyNode::getParent

Returns the parent node of the current node

说明

public <span class="type">tidyNodenull <span class="methodname">tidyNode::getParent ( <span class="methodparam">void )

Returns the parent node of the current node.

返回值

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

范例

示例 #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"

tidyNode::hasChildren

Checks if a node has children

说明

public bool tidyNode::hasChildren ( <span class="methodparam">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 ( <span class="methodparam">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 ( <span class="methodparam">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 ( <span class="methodparam">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 an element node

说明

public bool tidyNode::isHtml ( <span class="methodparam">void )

Tells if the node is an element node, but not the root node of the document.

返回值

Returns true if the node is an element node, but not the root node of the document, false otherwise.

更新日志

版本 说明
7.3.24, 7.4.12 This function has been fixed to have reasonable behavior. Previously, almost any node was reported as being an HTML node.

范例

示例 #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
<title></title>


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

tidyNode::isJste

Checks if this node is JSTE

说明

public bool tidyNode::isJste ( <span class="methodparam">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 ( <span class="methodparam">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 ( <span class="methodparam">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

本站为非盈利网站,作品由网友提供上传,如无意中有侵犯您的版权,请联系删除