重複の可能性:
PHP で HTML を解析および処理する方法は?
HTML hx (h1、h2、h3、...、h6) タグを 1 つ減らす PHP 関数を探しています。
- h1 が h2 になる
- h2 が h3 になるなど
- ...
- h6 は ' ' に置き換えられます
あなたはそのような機能を知っていますか?
これが、h6タグの削除を開始した方法です。
$string = preg_replace('#<(?:/)?\s*h6\s*>#', ' ', $string);
重複の可能性:
PHP で HTML を解析および処理する方法は?
HTML hx (h1、h2、h3、...、h6) タグを 1 つ減らす PHP 関数を探しています。
あなたはそのような機能を知っていますか?
これが、h6タグの削除を開始した方法です。
$string = preg_replace('#<(?:/)?\s*h6\s*>#', ' ', $string);
これはDOM
、すべてのマッピングを反復処理してから、タグを置き換えるか、子をコピーします。
<?php
// New tag mappings:
// null => extract childs and push them into parent contrainer
// Make sure that they are in this order, otherwise they would match wrongly
// between each another
$mapping = array(
'h6' => null,
'h5' => 'h6',
'h4' => 'h5',
'h3' => 'h4',
'h2' => 'h3',
'h1' => 'h2'
);
// Load document
$xml = new DOMDocument();
$xml->loadHTMLFile('http://stackoverflow.com/questions/12883009/php-code-to-decrease-html-hx-tags') or die('Failed to load');
$xPath = new DOMXPath( $xml);
foreach( $mapping as $original => $new){
// Load nodes
$nodes = $xPath->query( '//' . $original);
// This is a critical error and should NEVER happen
if( $nodes === false){
die( 'Malformed expression: //' . $original);
}
echo $original . ' has nodes: ' . $nodes->length . "\n";
// Process each node
foreach( $nodes as $node){
if( $new == null){
// Append all the childs before self and remove self afterwards
foreach( $node->childNodes as $child){
$node->parentNode->insertBefore( $child->cloneNode( true), $node);
}
$node->parentNode->removeChild( $node);
} else {
// Create new empty node and push all childrens to it
$newNode = $xml->createElement( $new);
foreach( $node->childNodes as $child){
$newNode->appendChild( $child);
}
$node->parentNode->replaceChild( $newNode, $node);
}
}
}
echo $xml->saveHTML();
またはを使用してチェックするなどのxPath
最適化を行うこともできますが、これを簡単にしたかったのです。//*
//h3|//h2
DOMElement::tagName
<?php
// The beginning (everything to the first foreach loop) remains the same
// Load nodes
$nodes = $xPath->query( '//*');
// This is a critical error and should NEVER happen
if( $nodes === false){
die( 'Malformed expression: //' . $original);
}
// Process each node
foreach( $nodes as $node){
// Check correct $node class
if( !($node instanceof DOMElement)){
continue;
}
$tagName = $node->tagName;
// Do we have a mapping?
if( !array_key_exists( $tagName, $mapping)){
continue;
}
$new = $mapping[$tagName];
echo 'Has element: ' . $tagName . ' => ' . $new . "\n";
if( $new == null){
// Append all the childs before self and remove self afterwards
foreach( $node->childNodes as $child){
$node->parentNode->insertBefore( $child->cloneNode( true), $node);
}
$node->parentNode->removeChild( $node);
} else {
// Create new empty node and push all childrens to it
$newNode = $xml->createElement( $new);
foreach( $node->childNodes as $child){
$newNode->appendChild( $child);
}
$node->parentNode->replaceChild( $newNode, $node);
}
}
echo $xml->saveHTML();
そして、私が考えることができる最後の最適化は、以下を使用することです:
$xPathQuery = '//' . implode( array_keys($mapping), '|//');
$nodes = $xPath->query( $xPathQuery);