1

何百もの特殊文字 (全角ダッシュ、スマート アポストロフィー、アクセント記号など) を含む html ドキュメントがあり、それらを同等の html に変換したいと考えています。

たとえば、ドキュメントに「em ダッシュ」(—) が含まれている場合、これを次のように変換したいと考えています。

 —

もちろん、私の html ドキュメントには html タグが含まれています。HTML タグの一部 ("<" や ">" など) を同等の HTML タグに変換したくありません。

HTML ドキュメントをアップロードできるツール (php スクリプト、Web アプリケーション、デスクトップ アプリケーションなど) はありますか?

多くの特殊文字を含む多くのドキュメントがあります。解決策として(特殊文字ごとに)「検索と置換」を使用する必要は避けたいと思います...時間がかかりすぎます。

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 に答える