PHP Conference Japan 2024

tidy::__construct

(PHP 5, PHP 7, PHP 8, PECL tidy >= 0.5.2)

tidy::__construct建構新的 tidy 物件

說明

public tidy::__construct(
    ?string $filename = null,
    array|string|null $config = null,
    ?string $encoding = null,
    bool $useIncludePath = false
)

建構新的 tidy 物件。

參數

filename

如果提供了 filename 參數,此函式也會讀取該檔案並使用該檔案初始化物件,如同 tidy_parse_file() 的行為。

config

config 可以傳遞為陣列或字串。如果傳遞字串,則會將其解讀為組態檔的名稱,否則會將其解讀為選項本身。

關於每個選項的說明,請造訪 » http://api.html-tidy.org/#quick-reference

encoding

encoding 參數設定輸入/輸出文件的編碼。編碼的可能值為:asciilatin0latin1rawutf8iso2022macwin1252ibm858utf16utf16leutf16bebig5shiftjis

useIncludePath

include_path 中搜尋檔案。

錯誤/例外

當建構函式失敗時(例如,無法開啟檔案)拋出例外。

變更記錄

版本 說明
8.4.0 執行建構函式時的失敗現在會拋出例外,而不是靜默地建立無法使用的物件。
8.0.0 filenameconfigencodinguseIncludePath 現在可為 null。

範例

範例 1 tidy::__construct() 範例

<?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>

另請參閱

新增註解

使用者貢獻的註解

此頁面沒有使用者貢獻的註解。
To Top