2014-10-23 2 views
1

Tidy를 사용하여 이전 시스템의 일부 콘텐츠를 정리하고 전송하려고합니다.Tidy에서 인라인 스타일을 제거하는 방법

시스템에는 완전히 삭제하려는 인라인 스타일 재정의가 많이 있습니다 (클래스로 변환하고 싶지 않고 삭제하려는 경우).

나는 다음과 같은 설정 사용하고 있습니다 :

$config = array(
    'indent'   => true, 
    'output-xhtml' => true, 
    'drop-font-tags' => true, 
    'clean' => true, 
    'merge-spans'=> true, 
    'drop-proprietary-attributes'=> true, 
); 

을 그리고 다음과 같이 실행 :

$test = '<p><span style="font-size: 10px;">Some content goes here.</span></p>'; 

$tidy = new tidy; 
$tidy->parseString($test, $config, 'utf8'); 
$body = $tidy->body(); 
var_dump($body->value); 

을하지만 출력은 여전히 ​​:

<body> 
    <p> 
    <span style="font-size: 10px;">Some content goes here.</span> 
    </p> 
</body> 

어떻게 단정받을 수 있나요 style="font-size: 10px;" 부품을 제거하거나 span 태그를 모두 삭제하십시오.

나는 그것을 할 documentation에서 다른 것을 볼 수 없습니다. 당신은 단지 스타일을 제거 할 수

답변

1

자신을 속성 :

$test = '<p><span style="font-size: 10px;">Some content goes here.</span></p>'; 
$dom = new DOMDocument;     
$dom->loadHTML($test);     
$xpath = new DOMXPath($dom);   
$nodes = $xpath->query('//*[@style]'); // Find elements with a style attribute 
foreach ($nodes as $node) {    
    $node->removeAttribute('style'); // Remove style attribute 
} 
$test = $dom->saveHTML(); 
$tidy = new tidy; 
$tidy->parseString($test, $config, 'utf8'); 
$body = $tidy->body(); 
var_dump($body->value);     
+0

니스 데이브, 나는 그것을 좋아한다. – Ben

+1

사이드 노트에서 원하지 않는 속성을 제거 할 수 없다면 깔끔한 부분이 없어 보입니다. – Ben

관련 문제