0

要使用PHP Simple HTML DOM Parser删除输入类型的 html 标签,我尝试了以下方式:

// Get DOM from URL or file

$html=new simple_html_dom();

// Loads the file from URL

$html->load_file($sourceurl);

// Remove input type text tag

    foreach ($html->find('input[type="text"]') as $e) {

        $e->outertext = '';
    }

我也知道这个链接:简单的 HTML Dom:如何删除元素?

但它不起作用。还有其他方法/解决方法吗?

编辑:如要求,这里是完整的源代码:

// Gets the url of the website to be parsed

$sourceurl=$_GET('task');

// Get DOM from URL or file

$html=new simple_html_dom();

// Loads the file from URL

$html->load_file($sourceurl);

// Remove input type text tag

    foreach ($html->find('input[type="text"]') as $e) {

        $e->outertext = '';
    }
echo $html;
4

2 回答 2

0

上面的代码应该可以工作。您是否尝试$html在删除标签后输出变量?

echo (string) $html; // Should print the html content without input tags
于 2013-11-06T05:43:32.150 回答
0

我对 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 );
于 2013-11-06T07:28:05.740 回答