调用未定义的函数 App\Helpers\htmlpurifier()

Call to undefined function App\Helpers\htmlpurifier()

提问人:DLK 提问时间:6/26/2022 更新时间:6/26/2022 访问量:399

问:

我正在尝试将 HTMLpurifier 集成到 codeigniter 中,用于文本编辑器。

我创建了一个名为它的助手,并在其中添加了净化器功能,如下所示:App/Helpershtmlpurifier_helper.php

defined('BASEPATH') or exit('No direct script access allowed');

if (!function_exists('html_purify')) {
  public function html_purify($dirty_html, $config = false)
  {
      if (is_array($dirty_html)) {
          foreach ($dirty_html as $key => $val) {
              $clean_html[$key] = html_purify($val, $config);
          }
      } else {
          $ci = &get_instance();

          switch ($config) {
              case 'comment':
                  $config = \HTMLPurifier_Config::createDefault();
                  $config->set('Core.Encoding', $ci->config->item('charset'));
                  $config->set('HTML.Allowed', 'p,a[href|title],abbr[title],acronym[title],b,strong,blockquote[cite],code,em,i,strike');
                  $config->set('AutoFormat.AutoParagraph', true);
                  $config->set('AutoFormat.Linkify', true);
                  $config->set('AutoFormat.RemoveEmpty', true);
                  break;

              case false:
                  $config = HTMLPurifier_Config::createDefault();
                  $config->set('Core.Encoding', $ci->config->item('charset'));
                  $config->set('Core.Encoding', 'utf-8');
                  $config->set("AutoFormat.AutoParagraph", false);
                  $config->set("Core.NormalizeNewlines", true);
                  $config->set('HTML.Allowed', 'iframe[src|title|frameborder|allowfullscreen|class|width|height|loading],p,b,strong,a[href|title],abbr[title],blockquote[cite],code,pre[class],em,i,strike,u,s,sub,sup,ol,ul,li,hr,img[title|alt|src|class|style],h1,h2,h3,h4,h5,h6,object[width|height|data],param[name|value],embed[src|type|allowscriptaccess|width|height],br,*[style]');
                  $config->set('CSS.AllowedProperties', 'font,font-size,font-weight,font-style,font-family,text-decoration,margin-left,margin-right,float,color,background-color,text-align,width,max-width');
                  $config->set('HTML.MaxImgLength', NULL);
                  $config->set('CSS.MaxImgLength', NULL);
                  $config->set('HTML.SafeObject', true);
                  $config->set('HTML.SafeEmbed', true);
                  $config->set('Output.FlashCompat', true);
                  $config->set('AutoFormat.RemoveEmpty', true);
                  $config->set('AutoFormat.RemoveEmpty.RemoveNbsp', true);
                  $config->set('HTML.SafeIframe', true);
                  $config->set('URI.SafeIframeRegexp', '%^//(www.youtube(?:-nocookie)?.com/embed/|player.vimeo.com/video/)%');
                  $def = $config->getHTMLDefinition(true);
                  $def->addAttribute('iframe','allowfullscreen', 'loading', 'Bool');
      
                  break;

              default:
                  show_error('The HTMLPurifier configuration labeled "'.htmlspecialchars($config, ENT_QUOTES, $ci->config->item('charset')).'" could not be found.');
          }
          
          require_once(APPPATH."app/ThirdParty/htmlpurifier/HTMLPurifier.auto.php"); 
          require_once(APPPATH."app/ThirdParty/htmlpurifier/HTMLPurifier.func.php");

          $purifier = new \HTMLPurifier($config);
          $clean_html = $purifier->purify($dirty_html);
      }

      return $clean_html;
  }
}

/* End of htmlpurifier_helper.php */
/* Location: ./app/helpers/htmlpurifier_helper.php */

我在控制器中调用它来使用它,如下所示:

$val->setRule('content', translation("content"), 'required');
$val = \App\Helpers\htmlpurifier;
// Or
$val = \App\Helpers\htmlpurifier();
$val = html_purify($val, 'comment');

我收到未定义的错误,我对 CodeIgniter 不熟悉。我想要的只是净化文本编辑器的内容。

感谢您的帮助。

php codeigniter-4 htmlpurifier

评论

0赞 steven7mwesigwa 6/26/2022
你真的在使用问题标签中指定的 CodeIgniter-4 吗?根据该方法的使用,源代码看起来像是“CodeIgniter-3”的源代码。get_instance();
0赞 DLK 6/26/2022
@steven7mwesigwa是的,我正在使用 4,正如我所说,我对 codeigniter 并不友好,从导师那里学习:)CodeIgniter 4 framework.

答:

1赞 steven7mwesigwa 6/26/2022 #1

而不是:

// ...
$val = \App\Helpers\htmlpurifier; ❌
// Or
$val = \App\Helpers\htmlpurifier(); ❌
$val = html_purify($val, 'comment');

使用这个: ✅

// ...

helper('htmlpurifier');
$val = html_purify($val, 'comment');

资源:加载帮助程序


补遗:

此外,删除,因为这仅适用于 CodeIgniter-3。$ci = &get_instance();

最后:-

而不是:❌

// ...
$ci->config->item('charset')

使用这个: ✅

config(\Config\App::class)->charset

资源:使用配置文件