Home > Article > Backend Development > How to output mathematical formulas in php code
使用google的接口显示数学公式
单行公式 (推荐学习:PHP视频教程)
<img src="http://latex.codecogs.com/gif.latex?$(a_{1})^2+(a_{3})^2\geq%202(a_{2})^2$" / alt="How to output mathematical formulas in php code" >
多行公式
<img src="http://latex.codecogs.com/gif.latex?$$%20\left\{%20\begin{aligned}%200\leq%20x\leq%202%20\\%200\leq%20y\leq%202\\%20\end{aligned}%20\right.%20$$" / alt="How to output mathematical formulas in php code" >
$str='$$ \left\{ \begin{aligned} x^2+3y^2=3 \\ y=k(x-1)\\ \end{aligned} \right. $$'; //将$$转化为$$ $str=str_replace("$$", "$", $str); // dd($str); $pattern='/\$(.*)\$/Us';//只能识别$$ $$ preg_match_all($pattern,$str,$matches); $arr_split=preg_split($pattern,$str); //dd($arr_split); // dd($matches); $count=count($matches[1]); $str_new=""; for($i=0;$i<count($matches[1]);$i++){ //$arr_replace[]='<img src="http://latex.codecogs.com/gif.latex?'.$matches[1][$i].'" / alt="How to output mathematical formulas in php code" >'; $matches_str=$matches[1][$i]; // dd($str); //先把\\n转换成\\\n $matches_str=str_replace("\\\n", "\\\\\n", $matches_str); // dd($matches_str); $matches_str=preg_replace('/\s*/', '', $matches_str); // dd($matches_str); $str_new.=$arr_split[$i]; $str_new.='<img src="http://latex.codecogs.com/gif.latex?'.$matches_str.'" / alt="How to output mathematical formulas in php code" >'; } if($count>0){ echo $str_new; }else{ echo $str; }
The above is the detailed content of How to output mathematical formulas in php code. For more information, please follow other related articles on the PHP Chinese website!