Chinaunix首页 | 论坛 | 博客
  • 博客访问: 601083
  • 博文数量: 129
  • 博客积分: 8026
  • 博客等级: 中将
  • 技术积分: 1300
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-21 14:39
文章分类

全部博文(129)

文章存档

2011年(1)

2007年(26)

2006年(102)

我的朋友

分类:

2006-06-09 11:40:29

Tidy is a binding for the Tidy HTML clean and repair utility
Tidy是一种整理和修复你的HTML的工具,支持面向对象或面向过程方式调用,下面简单说说它的用法:
1)对象方式


$html = '

test'; //要处理的字串
$tidy = new tidy;
$tidy->parseString($html);
$tidy->CleanRepair();
echo tidy_get_output($tidy); //输出处理后的结果
?>


2)过程方式

$html ="

error
another line";
$config = array('indent' => TRUE,
                'output-xhtml' => TRUE,
                'wrap' => 200);
$tidy = tidy_parse_string($buffer, $config, 'UTF8'); //返回的是tidy对象
$tidy->CleanRepair($tidy);
echo tidy_get_output($tidy); //输出处理后的结果
?>

$html ="

error
another line";
$config = array('indent' => TRUE,
                'output-xhtml' => TRUE,
                'wrap' => 200);
$tidy = tidy_repair_string($html, $config, 'UTF8'); //返回的是tidy对象
echo $tidy;
?>

其它函数:
tidy tidy_parse_file //需要用tidy_get_X系列处理返回
String tidy_repair_file  //可以直接echo
tidy tidy_parse_string
String tidy_repair_string
如果你需要单独处理HTML内容的某些指定标记,需要用tidy_parse_*配合tidy_clean_repair使用,并使用tidy_get_*系列输出结果(tidy_get_*接受的参数为tidy对象)
如果只是对整个内容简单处理并输出,直接用tidy_repair_*处理并返回结果字串,再echo就可以了

tidy_get_body  //返回body标记部分
tidy_get_config //返回参数配置 ,具体配置请参阅
tidy_get_head //head标记部分
tidy_get_html //html部分(全文?)

阅读(3025) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-06-08 14:31:10

补充 by wenchao: tidy不支持直接使用“gb2312”、“gbk”编码,必须用上面的命令转换,中文绝无乱码! 之所以写在这里,是因为本帖在Google搜索结果里很靠前,在这里分享经验可以方便很多人,呵呵!

chinaunix网友2009-06-08 14:22:49

补充 by wenchao: 在Windos下用Tidy转换带中文的HTML最规范的命令是: tidy -asxml -raw b.html (-raw表示直接输出127字符值的文字,不会把中文转换成乱码)