1

我有一个 html 文档,其中包含数百个特殊字符(如破折号、智能撇号、重音 egrave 等),我想将其转换为它们的 html 等价物。

例如,我的文档包含一个“破折号”(—),我想将其转换为:

 —

当然,我的 html 文档包含 html 标签。我不想将部分 html 标记(例如“<”或“>”)转换为 html 等效项。

是否有任何工具(php 脚本、Web 应用程序、桌面应用程序等)可以上传我的 html 文档,并返回相同的文档,但根据需要进行修改以包含 html 等效项?

我有很多文件,有很多特殊字符。我想避免必须使用“查找和替换”(对于每个特殊字符)作为解决方案......这将花费太长时间。

4

3 回答 3

1
$new = str_replace(array('&lt;', '&gt;'), array('<', '>'), htmlentities($old));
于 2010-08-10T10:23:15.387 回答
1

你可以使用类似的东西:

<?php
ob_start();
include 'test.html';
$content = ob_get_contents();
ob_clean();
$new = str_replace('<','$start$',$content);
$new = str_replace('>','$end$',$new);
$new = htmlentities($new);
$new = str_replace('$start$','<',$new);
$new = str_replace('$end$','>',$new);
echo $new;
ob_end_flush();
?>

然后只需将 test.html 更改为您要删除特殊字符的文件

编辑:这与同一目录中的每个 html 文件都自动化相同:

<?php
foreach(glob('*.html') as $file){
ob_start();
include $file;
$content = ob_get_contents();
ob_clean();
$new = str_replace('<','$start$',$content);
$new = str_replace('>','$end$',$new);
$new = htmlentities($new);
$new = str_replace('$start$','<',$new);
$new = str_replace('$end$','>',$new);
$file = fopen($file,'w');
fwrite($file,$new);
fclose($file);
}
echo 'done';
ob_end_flush();
?>
于 2010-08-03T03:48:03.120 回答
0

如果您仍然想这样做:

使用它们各自的代码创建一个特殊字符列表:

例如:

$htmlNumbers = array( "0" => array( "char"=>"—", "code"=>"&#8212" ),
                      "1" => array( "char"=>"@", "code"=>"&#64" ),
                      ---------------------
                      --------------------- 
                    );

现在从 html 文件中获取 html 内容并使用 str_replace 将所有字符替换为它们的代码:

$html = file_get_contents("index.html");

for( $i=0; $i<count( $htmlNumbers ); $i++ ) {                    
    $html = str_replace( $htmlNumbers[$i]['char'] , $htmlNumbers[$i]['code'], $html );
}

echo $html;

现在您可以使用文件处理方法将输出保存到 html 文件中。

于 2010-08-02T19:05:54.417 回答