如何在php / HTML中显示数学方程式?

问题描述

我有一个ckeditor,当我从数据库提取方程式时,我们可以在其中创建数学方程式,它看起来像a+b2,但我想像(a+b)^2。那么,我该怎么做?请帮助我。

html_entity_decode($post->answer)

我正在使用html_entity_decode,但看起来并不是我想要的。

解决方法

Browser support for MathML仍然不完整,但是您可以通过将MathJax库添加到Web文档中来使其在几乎任何浏览器中运行。只需将以下两行添加到您的HTML中:

<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>

然后应正确呈现。尝试运行以下代码段,例如:

<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<p>&nbsp;<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><msup><mfenced><mrow><mi>a</mi><mo>+</mo><mi>b</mi></mrow></mfenced><mn>2</mn></msup><mo>=</mo><msup><mi>a</mi><mn>2</mn></msup><mo>+</mo><msup><mi>b</mi><mn>2</mn></msup><mo>+</mo><mn>2</mn><mi>a</mi><mi>b</mi></mrow></math></p>