php - 阻止 PHP 异常转义 html
问题描述
我有一个自定义异常类,例如:
class AwesomeException extends \Exception {
public function __toString(){
return "<strong>This is important!</strong>";
}
然后我throw new \AwesomeException("Blah blah");
& 而不是显示:
这个很重要!
粗体字,
输出包括:
<strong>This is Important!</strong>
当我查看源代码时,我看到:
<strong>This is Important!</strong>
我尝试了覆盖getMessage()
,但这是最终的,所以它不起作用。
我可以使用自定义输出进行全局尝试/捕获,例如:
try {
//do all of the things
}
catch (\Exception $e){
echo $e;
echo $e->getTraceAsString();
}
但我真的很想用 htmlthrow $e
打印原件__toString
,而不是转义。我希望AwesomeException
班级以某种方式强制执行这一点。
我也可以echo
从我的内部 html __toString()
,但这打破了预期__toString()
我在 PHP 7.4
编辑
我在想这可能是apache?我.htaccess
的文件路径deliver.php
,所以我把文件的开头写成:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
class AwesomeException extends \Exception {
public function __toString(){
return "<strong>This is important!</strong>";
}
}
throw new \AwesomeException();
echo 'done throwing';
exit;
我得到了输出源:
<br />
<b>Fatal error</b>: Uncaught <strong>This is important!</strong>
thrown in <b>/path/to/file/deliver.php</b> on line <b>11</b><br />
解决方案
我phpinfo()
为“错误”、“异常”、“htmlentities”和其他几个运行了 ctrl+f 并最终找到了html_errors
指令。
所以我:
ini_set('html_errors',0);
现在我的异常中的 html 正在以未转义的形式打印。不过,它不再加粗我的文件名或“致命错误”。
推荐阅读
- r - 将表格 grob 居中对齐到 x 轴标签
- javascript - Flask 将 EventStreams 发送到 HTML 和 JS 获取事件消息
- bash - 如何将变量设置为 Bash 中两个命令的输出
- mongodb - 命令“mongod --auth --bind_ip_all”无限运行并消耗太多RAM - Mongo with Docker
- reactjs - 如何在 Apexchart 中自定义工具提示的标题和正文?
- python - Python3.8 `all()` 不短路
- reactjs - jsPDF 中的矩阵类在 React 中无法访问
- javascript - 想确认对使用 create-react-app 导入 index.js 的默认行为的理解
- node.js - 如何使用 discord.js 检查有多少人有角色?
- vue.js - vue-router 嵌套路由和多个路由器视图没有内容