MacOS下XAMPP启用Tidy扩展仍报Class 'tidy'未找到的解决方法
解决MacOS XAMPP中Tidy类找不到的问题
问题描述
Windows系统的XAMPP中,只需在php.ini启用Tidy扩展即可正常使用Tidy功能,但在MacOS的XAMPP中,已在/Applications/XAMPP/xamppfiles/etc/php.ini中启用该扩展,实例化tidy类时仍报错:
Fatal error: Uncaught Error: Class "tidy" not found in /Applications/XAMPP/xamppfiles/htdocs/joacmedia/app/core/functions.php:48 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/joacmedia/app/models/user.php(402): truncate('James is a powe...', 200) #1 /Applications/XAMPP/xamppfiles/htdocs/joacmedia/app/views/joacmedia/temp/us.php(237): User->truncate('James is a powe...', 200) #2 /Applications/XAMPP/xamppfiles/htdocs/joacmedia/app/views/joacmedia/temp/us.php(283): Page->sectionIntro(Object(stdClass)) #3 /Applications/XAMPP/xamppfiles/htdocs/joacmedia/app/views/joacmedia/temp/us.php(34): Page->LayoutBottomAream(Object(stdClass)) #4 /Applications/XAMPP/xamppfiles/htdocs/joacmedia/app/views/joacmedia/temp/us.php(355): Page->__construct(Array) #5 /Applications/XAMPP/xamppfiles/htdocs/joacmedia/app/core/controller.php(11): include('/Applications/X...') #6 /Applications/XAMPP/xamppfiles/htdocs/joacmedia/app/views/joacmedia/us.php(5): Controller->view('joacmedia/temp/...', Array) #7 /Applications/XAMPP/xamppfiles/htdocs/joacmedia/app/core/controller.php(11): include('/Applications/X...') #8 /Applications/XAMPP/xamppfiles/htdocs/joacmedia/app/controllers/us.php(47): Controller->view('joacmedia/us', Array) #9 /Applications/XAMPP/xamppfiles/htdocs/joacmedia/app/core/app.php(61): Us->index('jay') #10 /Applications/XAMPP/xamppfiles/htdocs/joacmedia/index.php(6): App->__construct() #11 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/joacmedia/app/core/functions.php on line 48
相关truncate函数代码:
//add ellipsis to string function truncate($html,$length){ $tidy = new tidy(); if ((int)mb_strlen($html,'UTF-8') >= (int)$length ) { $ellipsis = "<div class='ellipsis_show'> <div data-href='false' class='text-right see_more_text text-secondary'><strong>...</strong>see more</div> </div>"; return trim((strip_empty_tag(convert_ENCODE($tidy->repairString("<span data-show-summary='true'>".(NewLineToBreak(substr(trim($html),0,$length)))."</span>"."<span class='text_exposed_hide hide visually_hidden'>".trim(NewLineToBreak(substr($html,$length)))."</span>"),'UTF-8')))).$ellipsis; } return trim((strip_empty_tag(convert_ENCODE(tidy_repair_string(NewLineToBreak($html)),'UTF-8')))); }
解决步骤
1. 确认php.ini修改生效
- 打开
/Applications/XAMPP/xamppfiles/etc/php.ini,找到;extension=tidy,去掉前面的分号,保存文件。 - 必须重启XAMPP的Apache服务,修改php.ini后不重启服务,扩展不会加载。
2. 验证扩展是否存在并加载
- 查看XAMPP的PHP扩展目录(路径类似
/Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-xxxxxx/,xxxxxx是对应PHP版本的标识),确认存在tidy.so文件。 - 在网站根目录创建
phpinfo.php,内容为:
<?php phpinfo(); ?>
访问该文件,搜索tidy,如果能找到Tidy相关的配置模块,说明扩展加载成功;如果没有,继续下一步。
3. 排查PHP环境混淆问题
MacOS自带PHP,可能出现XAMPP的PHP和系统PHP混淆的情况:
- 确保XAMPP的Apache使用的是自身的PHP版本,而非系统自带的。可以在XAMPP控制面板中检查Apache的配置,确认PHP路径指向
/Applications/XAMPP/xamppfiles/bin/php。
4. 代码层面临时修复
如果以上步骤都无法解决,可以修改代码,避免实例化tidy类,改用全局函数tidy_repair_string()替代:
修改truncate函数中的$tidy->repairString()为tidy_repair_string(),去掉实例化代码:
//add ellipsis to string function truncate($html,$length){ if ((int)mb_strlen($html,'UTF-8') >= (int)$length ) { $ellipsis = "<div class='ellipsis_show'> <div data-href='false' class='text-right see_more_text text-secondary'><strong>...</strong>see more</div> </div>"; return trim((strip_empty_tag(convert_ENCODE(tidy_repair_string("<span data-show-summary='true'>".(NewLineToBreak(substr(trim($html),0,$length)))."</span>"."<span class='text_exposed_hide hide visually_hidden'>".trim(NewLineToBreak(substr($html,$length)))."</span>"),'UTF-8')))).$ellipsis; } return trim((strip_empty_tag(convert_ENCODE(tidy_repair_string(NewLineToBreak($html)),'UTF-8')))); }
内容的提问来源于stack exchange,提问作者Jay Oduro
相关产品推荐
相关产品推荐

