簡體   English   中英

PHP簡單HTML DOM解析器不會刪除輸入類型html標記

[英]Input type html tags are not being removed by PHP Simple HTML DOM Parser

為了使用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;

上面的代碼應該工作。 您是否在刪除標簽后嘗試輸出$html變量?

echo (string) $html; // Should print the html content without input tags

我不熟悉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 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM