1

我有课ClassA,想在这个课上使用 simple_html_dom 的函数。我怎样才能做到这一点?这是 simple_html_dom 类的链接http://www.megafileupload.com/en/file/366382/simple-html-dom-rar.html

4

1 回答 1

3
<?php
    Class A
    {
        private $simpleHTML;

        function __construct()
        {
            $this->simpleHTML = new simple_html_dom(); 
            //now you can call all simple html functions using $this->simpleHTML->..
        }

        //define file_get_html as a class method. You can call this as 
        // $x = new A();
        //$x->file_get_html..(externally) or $this->file_get_html(.. (internally)
        function file_get_html($url, 
                        $use_include_path = false, 
                        $context=null, $offset = -1, 
                        $maxLen=-1, $lowercase = true, 
                        $forceTagsClosed=true, 
                        $target_charset = DEFAULT_TARGET_CHARSET, 
                        $stripRN=true, 
                        $defaultBRText=DEFAULT_BR_TEXT)
        {
            // We DO force the tags to be terminated.
            $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $defaultBRText);
            // For sourceforge users: uncomment the next line and comment the retreive_url_contents line 2 lines down if it is not already done.
            $contents = file_get_contents($url, $use_include_path, $context, $offset);
            // Paperg - use our own mechanism for getting the contents as we want to control the timeout.
            //$contents = retrieve_url_contents($url);
            if (empty($contents))
            {
                return false;
            }
            // The second parameter can force the selectors to all be lowercase.
            $dom->load($contents, $lowercase, $stripRN);
            return $dom;
        }
    }
?>
于 2012-08-31T04:11:24.800 回答