php教程

tidy::parseString

tidy_parse_string

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

tidy::parseString -- tidy_parse_stringParse a document stored in a string

说明

面向对象风格

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

过程化风格

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

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.

范例

Example #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>

参见

User Contributed Notes

steven at nevvix dot com 09-Aug-2018 03:36
<?php
/**
 * Simpler version without pretty print config options.
 */
function tidy_html5($html, array $config = [], $encoding = 'utf8') {
   
$config += [
       
'doctype' => '<!DOCTYPE html>',
       
'drop-empty-elements' => 0,
       
'new-blocklevel-tags' => 'article aside audio bdi canvas details dialog figcaption figure footer header hgroup main menu menuitem nav section source summary template track video',
       
'new-empty-tags' => 'command embed keygen source track wbr',
       
'new-inline-tags' => 'audio command datalist embed keygen mark menuitem meter output progress source time video wbr',
       
'tidy-mark' => 0,
    ];
   
$html = tidy_parse_string($html, $config, $encoding); // doctype not inserted
   
tidy_clean_repair($html); // doctype inserted
   
return $html;
}

$html = '</z><p><a href="#">Link</a></p><p><img src="logo.png"/>Se?ond para</p><i class="fa"></i><p></p>';

echo
tidy_html5($html);

<!
DOCTYPE html>
<
html>
<
head>
<
title></title>
</
head>
<
body>
<
p><a href="#">Link</a></p>
<
p><img src="logo.png">Se?ond para</p>
<
i class="fa"></i>
<
p></p>
</
body>
</
html>

echo
tidy_html5($html, ['indent'=>2, 'indent-spaces'=>4]);

<!
DOCTYPE html>
<
html>
<
head>
    <
title></title>
</
head>
<
body>
    <
p><a href="#">Link</a></p>
    <
p><img src="logo.png">Se?ond para</p><i class="fa"></i>
    <
p></p>
</
body>
</
html>

echo
tidy_html5($html, ['indent'=>1], 'ascii');

<!
DOCTYPE html>
<
html>
  <
head>
    <
title></title>
  </
head>
  <
body>
    <
p>
      <
a href="#">Link</a>
    </
p>
    <
p>
      <
img src="logo.png">Se&Atilde;&sect;ond para
   
</p><i class="fa"></i>
    <
p></p>
  </
body>
</
html>

echo
tidy_html5($html, ['show-body-only'=>1]);

<
p><a href="#">Link</a></p>
<
p><img src="logo.png">Se?ond para</p>
<
i class="fa"></i>
<
p></p>
steven at nevvix dot com 09-Aug-2018 12:57
<?php
/**
* UTF-8 HTML5-compatible Tidy
*
* @param string $html
* @param array $config
* @param string $encoding
* @link http://tidy.sourceforge.net/docs/quickref.html
*/
function tidy_html5($html, array $config = [], $encoding = 'utf8') {
   
$config += [
       
'clean'       => TRUE,
       
'doctype'     => 'omit',
       
'indent'      => 2, // auto
       
'output-html' => TRUE,
       
'tidy-mark'   => FALSE,
       
'wrap'        => 0,
       
// HTML5 tags
       
'new-blocklevel-tags' => 'article aside audio bdi canvas details dialog figcaption figure footer header hgroup main menu menuitem nav section source summary template track video',
       
'new-empty-tags' => 'command embed keygen source track wbr',
       
'new-inline-tags' => 'audio command datalist embed keygen mark menuitem meter output progress source time video wbr',
    ];
   
$html = tidy_parse_string($html, $config, $encoding);
   
tidy_clean_repair($html);
    return
'<!DOCTYPE html>' . PHP_EOL . $html;
}

$html = '</z><p><a href="#">Link</a></p><p>Second para</p>';
echo
tidy_html5($html);

Output:
<!
DOCTYPE html>
<
html>
<
head>
  <
title></title>
</
head>
<
body>
  <
p><a href="#">Link</a></p>
  <
p>Second para</p>
</
body>
</
html>

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788