PHP脚本在Debian 11 LXC容器中输出无法换行问题求助
WordPress更新检测脚本换行异常排查与解决
我编写了一款检测WordPress更新状态的PHP脚本,部署在两台Debian 11 LXC容器中。其中一台容器的脚本输出正常,分3行展示核心、插件、主题的更新状态;但另一台容器的所有输出内容挤在一行,无法正常换行。
已尝试的操作:
- 使用
PHP_EOL作为换行符,也替换过"\n" - 将
print语句替换为echo - 确认两台容器的php.ini配置、已安装PHP模块完全一致
问题仍未解决,附上脚本代码寻求解决思路:
#!/usr/bin/env php <?php $path = '/var/www'; $update_core = false; $update_plugins = array(); $update_themes = array(); require_once("$path/wp-load.php"); global $wp_version; wp_update_plugins(); wp_version_check(); wp_update_themes(); $core = get_site_transient('update_core'); $plugins = get_site_transient('update_plugins'); $themes = get_site_transient('update_themes'); if ($themes) { foreach($themes->response as $theme) { array_push($update_themes, $theme['theme']); } } if ($plugins) { foreach($plugins->response as $plugin) { array_push($update_plugins, $plugin->slug); } } if ($core) { $arr_core = $core->updates; if (is_array($arr_core) && $arr_core[0]) { $obj_core = $arr_core[0]; if ($obj_core->response && $obj_core->response != "latest") { $update_core = $obj_core->current; } } } if ($update_core) { $installed_version_array = explode(".". $wp_version); $available_version_array = explode(".". $update_core); if ($available_version_array[0] > $installed_version_array[0] || $available_version_array[1] > $installed_version_array[1]) { print "2 \"Wordpress version\" - Wordpress version upgrade from $wp_version to $update_core". PHP_EOL; } else { print "1 \"Wordpress version\" - Wordpress version upgrade from $wp_version to $update_core". PHP_EOL; } } else { print "0 \"Wordpress version\" - Wordpress version $wp_version is up-to-date". PHP_EOL; } $pending_plugins = sizeof($update_plugins); if ($update_plugins) { $msg_plugins = "$pending_updates Wordpress plugins outdated (". join($update_plugins, ", "). ")"; } else { $msg_plugins = "All Wordpress plugins up-to-date"; } print "P \"Wordpress plugins\" pending_updates=$pending_plugins;1;3;; $msg_plugins". PHP_EOL; $pending_themes = sizeof($update_themes); if ($update_themes) { $msg_themes = "$pending_themes Wordpress themes outdated (". join($update_themes, ", "). ")"; } else { $msg_themes = "All Wordpress themes up-to-date"; } print "P \"Wordpress themes\" pending_updates=$pending_themes;1;;; $msg_themes". PHP_EOL; ?>
排查方向与解决办法
1. 清空WordPress加载的输出缓冲
WordPress的wp-load.php可能触发第三方插件/主题开启的输出缓冲,导致所有输出被暂存后一次性输出。在脚本开头添加以下代码强制关闭缓冲:
<?php // 清空所有层级的输出缓冲 while (ob_get_level() > 0) { ob_end_clean(); }
2. 强制使用LF换行符
部分容器环境可能存在PHP_EOL异常(比如被设置为CRLF),直接在输出中使用"\n"替代PHP_EOL:
// 示例:替换核心状态输出的换行符 print "0 \"Wordpress version\" - Wordpress version $wp_version is up-to-date\n";
3. 检查输出的换行符类型
用cat -A命令查看脚本输出的换行符细节,确认是否为LF(正常)或CRLF(异常):
php /path/to/your/script.php | cat -A
如果异常容器显示^M$(CRLF),说明环境变量或配置导致换行符被转换,可手动指定输出为LF。
4. 排查WordPress环境差异
异常容器中的WordPress可能加载了特殊插件/主题,干扰输出行为。临时切换到WordPress默认主题(如Twenty Twenty-Four)并禁用所有插件,再运行脚本测试。若恢复正常,逐个排查插件/主题的输出逻辑。
5. 强制PHP以纯CLI模式运行
修改脚本Shebang,强制PHP跳过php.ini配置(测试用),排查隐藏配置差异:
#!/usr/bin/env php -n
若测试后输出正常,重点检查output_handler、implicit_flush等php.ini参数的细微差异。
内容的提问来源于stack exchange,提问作者Peronia
相关产品推荐
相关产品推荐

