在 Codeigniter 3.0.1 中自动加载和使用简单的 HTML dom 外部库

Autoload and use simple HTML dom external librarie in Codeigniter 3.0.1

我在 CodeIgniter 3.0 中包含/使用外部库时遇到问题。这就是我到目前为止所做的:

我已将 simple_html_dom.php 文件放入我的 application/libraries 文件夹

然后我用

中的这行代码自动加载它
/*
example of CI 
$autoload['libraries'] = array('user_agent' => 'ua');
*/

$autoload['libraries'] = array('simple_html_dom' => 'shd');

这是我的控制器

public function index()
    {
        $html = $this->shd->str_get_html('<html><body>Hello!</body></html>');
        var_dump($html);
        die();
        $this->load->view('parser');
    }

这给我一个错误:

A PHP Error was encountered
Severity: Error
Message: Call to undefined method simple_html_dom::str_get_html()
Filename: controllers/Parser.php
Line Number: 8

可在此 link

上找到 simple_html_dom 的文档

对我来说,图书馆似乎已加载,但我无法使用其功能。 我希望有人能帮忙。提前致谢!

你可以使用

$this->load->library("simple_html_dom"); //class name should come here

并确保 simple_html_dom.php class 名称是 simple_html_dom

试试这个 -

$autoload['libraries'] = array('simple_html_dom');

我找到了解决办法。在检查了简单 html dom 的文档后,我发现您也可以使用面向对象的方式。所以我的控制器现在看起来像这样:

$html = new simple_html_dom();
$html->load('<html><body>Hello!</body></html>');
var_dump($html);

我得到了结果:

object(simple_html_dom)[17]
  public 'root' => 
    object(simple_html_dom_node)[18]
      public 'nodetype' => int 5
      public 'tag' => string 'root' (length=4)
      public 'attr' => 
        array (size=0)
          empty
      public 'children' => 
        array (size=1)
          0 => 
            object(simple_html_dom_node)[19]
              ...
      public 'nodes' => 
        array (size=1)
          0 => 
            object(simple_html_dom_node)[19]
              ...
      public 'parent' => null
      public '_' => 
        array (size=2)
          0 => int -1
          1 => int 4
      public 'tag_start' => int 0
      private 'dom' => 
        &object(simple_html_dom)[17]
  public 'nodes' => 
    array (size=4)
      0 => 
        object(simple_html_dom_node)[18]
          public 'nodetype' => int 5
          public 'tag' => string 'root' (length=4)
          public 'attr' => 
            array (size=0)
              ...
          public 'children' => 
            array (size=1)
              ...
          public 'nodes' => 
            array (size=1)
              ...
          public 'parent' => null
          public '_' => 
            array (size=2)
              ...
          public 'tag_start' => int 0
          private 'dom' => 
            &object(simple_html_dom)[17]

你可以这样做:

$this->load->library("simple_html_dom");
$this->simple_html_dom->your_method();