2024 日本 PHP 研討會

tidy::repairString

tidy_repair_string

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

tidy::repairString -- tidy_repair_string使用選用提供的設定檔修復字串

說明

物件導向風格

public static tidy::repairString(字串 $string, 陣列|字串|null $config = null, ?字串 $encoding = null): 字串|false

程序風格

tidy_repair_string(字串 $string, 陣列|字串|null $config = null, ?字串 $encoding = null): 字串|false

修復指定的字串。

參數

string (字串)

要修復的資料。

config (設定)

設定 config 可以以陣列或字串的方式傳遞。如果傳遞的是字串,它會被解釋為設定檔的名稱,否則,它會被解釋為選項本身。

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

encoding (編碼)

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

回傳值

回傳修復後的字串,失敗時回傳 false

更新日誌

版本 說明
8.0.0 tidy::repairString() 現在是一個靜態方法。
8.0.0 configencoding 現在可以為 null。
8.0.0 此函式不再接受 useIncludePath 參數。

範例

範例 #1 tidy::repairString() 範例

<?php
ob_start
();
?>

<html>
<head>
<title>測試</title>
</head>
<body>
<p>錯誤</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>

另請參閱

新增註釋

使用者貢獻的註釋 3 則註釋

4
gnuffo1 at gmail dot com
14 年前
您也可以使用此函數來修復 XML,例如,如果雜散的「&」符號等導致錯誤

<?php
$xml
= tidy_repair_string($xml, array(
'output-xml' => true,
'input-xml' => true
));
?>
1
Romolo
7 年前
使用 tidy 可以很容易地修復損壞的 ods/odt 文件
我寫了以下程式碼,可以從命令列執行

<?php
$zip
= new ZipArchive();
if (
$zip->open($argv[1])) {
$fp = $zip->getStream('content.xml'); //file inside archive
if(!$fp)
die(
"Error: can't get stream to document file");
$stat = $zip->statName('content.xml');
$buf = ""; //file buffer
ob_start(); //to capture CRC error message
while (!feof($fp)) {
$buf .= fread($fp, 2048);
}
$s = ob_get_contents();
ob_end_clean();
fclose($fp);
$zip->close();
$config = array(
'indent' => true,
'clean' => true,
'input-xml' => true,
'output-xml' => true,
'wrap' => false
);
$tidy = new Tidy();
$xml = $tidy->repairstring($buf, $config);
$array=split("\n",$xml);
$file=tempnam("/tmp","xml");
$fp=fopen($file,"rw+");
foreach (
$array as $key=>$value) {
fwrite($fp,trim($value),strlen(trim($value)));
if (
$key==0) {
fwrite($fp,"\n");
}
}
fclose($fp);
if (
$zip->open($argv[1]) === TRUE) {
$zip->deleteName('content.xml');
$zip->addFile($file, 'content.xml');
$zip->close();
echo
'recovery complete';
} else {
echo
'recovery failed';
}
unlink($file);
}
?>

將其儲存到名為 fixdoc 的檔案中,並以如下方式呼叫
php fixdoc yourbrokendoc

為了安全起見,請處理文件的副本。
0
dan-dot-hunsaker-at-gmail-dot-com
13 年前
上面 http://tidy.sourceforge.net/docs/quickref.html 中引用的文件指出,配置選項 'sort-attributes' 是 'none' 和 'alpha' 的列舉,因此指定了任何一種形式的字串都是可接受的值。然而,情況可能並非如此 - 在我的系統上,在我將其設定為 true 之前,該選項未被採用。其他選項也可能是這種情況,所以請多做一些實驗。tidy::getConfig() 的輸出在這方面可能很有用。
To Top