PHP-在div中显示mysql文本字段中的html
在从mysql数据库中的文本字段检索html标记后,我在显示html标记方面遇到了一些问题 我的代码如下所示:PHP-在div中显示mysql文本字段中的html,php,html,mysql,Php,Html,Mysql,在从mysql数据库中的文本字段检索html标记后,我在显示html标记方面遇到了一些问题 我的代码如下所示: <div class='textDiv'><? echo htmlspecialchars_decode(str_replace("\r\n", "<br>", $row["textField"]), ENT_NOQUOTES);?></div> 但是,脚本生成的HTML代码如下所示: <div class="textDiv"&g
<div class='textDiv'><? echo htmlspecialchars_decode(str_replace("\r\n", "<br>", $row["textField"]), ENT_NOQUOTES);?></div>
但是,脚本生成的HTML代码如下所示:
<div class="textDiv">Senior Advisor said in a <a href="test.html">media interview</a> that the overall monetary policy tone is shifting towards...</div>
高级顾问在一份声明中表示,总体货币政策基调正在向。。。
你们知道为什么会发生这种情况以及如何解决吗?因此,在使用
htmlspecialchars\u decode
时,文本将平滑显示,不会出现换行?因此,如果要保留decode功能
1) 将链接更改为普通链接的样子,例如:yourwebsite.com/test.html不添加标记
2) 不要直接回显字符串,请执行此操作
$text = htmlspecialchars_decode(str_replace("\r\n", "<br>", $row["textField"]);
$text=htmlspecialchars\u解码(str\u替换(“\r\n”,“
”,$row[“textField”);
3) 然后将变量$text传递给代码以检测链接,如下所示:-
$reg_exUrl = '@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.\,]*(\?\S+)?)?)*)@';
if(preg_match($reg_exUrl, $text, $url)) {
echo preg_replace($reg_exUrl, '<a href="https://'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text);
}
else
{
echo $text;
}
$reg\u exUrl='@((https?:/)([-\w]+\.[-\w\.]+)+\w(:\d+)(/([-\w/\.\,]*(\?\S+))*);
if(preg_match($reg_exUrl,$text,$url)){
echo preg_替换($reg_exUrl,,$text);
}
其他的
{
echo$文本;
}
您可以更改代码以适应您的需要您必须在CSS文件或代码中进行一些更改您为
元素提供了哪些样式?已修复,谢谢!它是在CSS中,我有一个带有display:block的通用a{}样式(对于另一个部分),添加了.textdivA{display:inline;},现在可以使用了。
$reg_exUrl = '@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.\,]*(\?\S+)?)?)*)@';
if(preg_match($reg_exUrl, $text, $url)) {
echo preg_replace($reg_exUrl, '<a href="https://'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text);
}
else
{
echo $text;
}