私は simple_html_dom() に慣れていませんが、DOMを使用できる場合は、私のクラスを使用できます。これはスナップショットにすぎませんが、必要なものが得られるはずです。
ここでも確認できます: http://sandbox.onlinephpfunctions.com/code/aa54bdbf416ae1726ef7ca675b2324c37626920b
これがクラス myDOMDocument() です
/**
* myDOMDocument
*
* This class extend the DOMDocument class with some helpful methods.
*
* @subpackage DOMDocument
* @version $Revision: 9497 $ $Date: 2007-12-19 17:03:25 +1000 (Tr, 19 Grd 2007) $ $Author: talisin $
*
*/
class myDOMDocument extends DOMDocument {
/**
* Load HTML with mb_convert_encoding before load UTF-8 page
* to ensure that the output is the same as the input
*
* @link http://www.php.net/manual/en/domdocument.loadhtml.php#74777
* @see DOMDocument::loadHTML()
*
* @param string $html
* @param string $encoding
*
*/
public function loadHTML($html, $encoding = "UTF-8") {
$html = mb_convert_encoding($html, 'HTML-ENTITIES', $encoding);
@parent::loadHTML($html); #suppress warnings
}
/**
* Return HTML while stripping the auto-added tags html, body, and doctype.
*
* @see DOMDocument::saveHTML()
* @since PHP/5.3.6
*
* @param bool $ignoreAutoAddTags (optional) strip the auto-added tags
*
*/
public function saveHTML( $ignoreAutoAddTags = false ) {
if( $ignoreAutoAddTags ) {
$content = preg_replace(array("/^\<\!DOCTYPE.*?<html><body>/si","!</body></html>$!si"),"",parent::saveHTML());
return $content;
}
return parent::saveHTML( parent::documentElement );
}
/**
* Delete a HTML tag by either a matching tag or additional by tag and his attributes
*
* @example $dom->deleteHtmlTag( 'input', array( 'type' => 'text' ) );
* This will delete all input type="text"
*
*
* @param string $tag_name Name of the HTML tag to delete
* @param array $attributes Array of attributes where the key = attribute name and value = attribute value
*/
public function deleteHtmlTag( $tag_name, $attributes = array() ) {
$remove_tag = array(); # holds the DOMNodes we want to delete
foreach (parent::getElementsByTagName($tag_name) as $tag) {
// if method call has attributes
if (count($attributes) > 0) {
// for each HTML attribute of the given node
foreach ($tag->attributes as $tag_attribute) {
// for each given attribute
foreach( $attributes as $name => $value ) {
if ($tag_attribute->name == $name && $tag_attribute->value == $value ) {
$remove_tag[] = $tag;
}
}
}
}
// otherwise delte the whole tag
else {
$remove_tag[] = $tag;
}
}
if ( count( $remove_tag ) > 0 ) {
foreach ($remove_tag as $tag) {
$tag->parentNode->removeChild($tag);
}
}
}
}
echo $html = '<div id="hello">Hello</div><input type="text" name="Sample1"><div id="world"><input type="submit" name="Sample2"></div>'.PHP_EOL.PHP_EOL;
$dom = new myDOMDocument();
$dom->loadHTML( $html );
$dom->deleteHtmlTag( 'input', array( 'type' => 'text' ) );
echo $dom->saveHTML( true );