序章
これまでに見た最良の解決策は、HTML Tidy
http://tidy.sourceforge.net/を使用することです。
Tidyは、ドキュメントの形式を変換するだけでなく、cleanオプションを使用して、廃止されたHTMLタグを対応するカスケードスタイルシート(CSS)に自動的に変換することもできます。生成された出力には、インラインスタイル宣言が含まれています。
また、HTMLドキュメントにxhtml
互換性があることを保証します
例
$code ='<p>
<strong>
<span style="font-size: 14px">
<span style="color: #006400">
<span style="font-size: 14px">
<span style="font-size: 16px">
<span style="color: #006400">
<span style="font-size: 14px">
<span style="font-size: 16px">
<span style="color: #006400">This is a </span>
</span>
</span>
</span>
</span>
</span>
</span>
<span style="color: #006400">
<span style="font-size: 16px">
<span style="color: #b22222">Test</span>
</span>
</span>
</span>
</span>
</strong>
</p>';
走ったら
$clean = cleaning($code);
print($clean['body']);
出力
<p>
<strong>
<span class="c3">
<span class="c1">This is a</span>
<span class="c2">Test</span>
</span>
</strong>
</p>
CSSを入手できます
$clean = cleaning($code);
print($clean['style']);
出力
<style type="text/css">
span.c3 {
font-size: 14px
}
span.c2 {
color: #006400;
font-size: 16px
}
span.c1 {
color: #006400;
font-size: 14px
}
</style>
私たちの完全なHTML
$clean = cleaning($code);
print($clean['full']);
出力
<!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></title>
<style type="text/css">
/*<![CDATA[*/
span.c3 {font-size: 14px}
span.c2 {color: #006400; font-size: 16px}
span.c1 {color: #006400; font-size: 14px}
/*]]>*/
</style>
</head>
<body>
<p>
<strong><span class="c3"><span class="c1">This is a</span>
<span class="c2">Test</span></span></strong>
</p>
</body>
</html>
使用した機能
function cleaning($string, $tidyConfig = null) {
$out = array ();
$config = array (
'indent' => true,
'show-body-only' => false,
'clean' => true,
'output-xhtml' => true,
'preserve-entities' => true
);
if ($tidyConfig == null) {
$tidyConfig = &$config;
}
$tidy = new tidy ();
$out ['full'] = $tidy->repairString ( $string, $tidyConfig, 'UTF8' );
unset ( $tidy );
unset ( $tidyConfig );
$out ['body'] = preg_replace ( "/.*<body[^>]*>|<\/body>.*/si", "", $out ['full'] );
$out ['style'] = '<style type="text/css">' . preg_replace ( "/.*<style[^>]*>|<\/style>.*/si", "", $out ['full'] ) . '</style>';
return ($out);
}
================================================
編集1:ダーティハック(非推奨)
================================================
あなたの最後のコメントに基づいて、あなたが減価償却スタイルを保持したいようなものです..HTML Tidy
それ以来、あなたはそれを行うことができないかもしれませんがdepreciated
、あなたはこれを行うことができます
$out = cleaning ( $code );
$getStyle = new css2string ();
$getStyle->parseStr ( $out ['style'] );
$body = $out ['body'];
$search = array ();
$replace = array ();
foreach ( $getStyle->css as $key => $value ) {
list ( $selector, $name ) = explode ( ".", $key );
$search [] = "<$selector class=\"$name\">";
$style = array ();
foreach ( $value as $type => $att ) {
$style [] = "$type:$att";
}
$replace [] = "<$selector style=\"" . implode ( ";", $style ) . ";\">";
}
出力
<p>
<strong>
<span style="font-size:14px;">
<span style="color:#006400;font-size:14px;">This is a</span>
<span style="color:#006400;font-size:16px;">Test</span>
</span>
</strong>
</p>
使用したクラス
//Credit : http://stackoverflow.com/a/8511837/1226894
class css2string {
var $css;
function parseStr($string) {
preg_match_all ( '/(?ims)([a-z0-9, \s\.\:#_\-@]+)\{([^\}]*)\}/', $string, $arr );
$this->css = array ();
foreach ( $arr [0] as $i => $x ) {
$selector = trim ( $arr [1] [$i] );
$rules = explode ( ';', trim ( $arr [2] [$i] ) );
$this->css [$selector] = array ();
foreach ( $rules as $strRule ) {
if (! empty ( $strRule )) {
$rule = explode ( ":", $strRule );
$this->css [$selector] [trim ( $rule [0] )] = trim ( $rule [1] );
}
}
}
}
function arrayImplode($glue, $separator, $array) {
if (! is_array ( $array ))
return $array;
$styleString = array ();
foreach ( $array as $key => $val ) {
if (is_array ( $val ))
$val = implode ( ',', $val );
$styleString [] = "{$key}{$glue}{$val}";
}
return implode ( $separator, $styleString );
}
function getSelector($selectorName) {
return $this->arrayImplode ( ":", ";", $this->css [$selectorName] );
}
}