我是 php 新手(已经接触了几天),但多年来一直在使用 c++ 和 python 等语言。我目前正在制作一个基本的全局聊天系统,其中输入的聊天内容通过 AJAX/php 发送到 mysql 数据库。聊天内容每 5 秒使用类似的方法更新到指定的 div 中。
在 GetChat.php 中,我在回显文本之前过滤文本,并尝试实现类似于 bbcode 系统的东西。但是,当我使用
<b>
标签或 <span>
标签时,它会破坏并搞乱文本的顺序。我可以了解如何正确完成这部分吗?
我的代码:
foreach ($chatarray as $chat) {
$newmessage = htmlentities($chat);
$newmessage = nl2br($newmessage);
$bbcode = array("[b]", "[/b]", "[red]", "[blue]", "[/color]");
$htmlcode = array("<b>", "</b>", "<span style='color:red'>", "<span style='color:blue'>","</span>");
$newmessage = str_replace($bbcode, $htmlcode, $newmessage);
$newmessage = wordwrap($newmessage, 34, "\n", true);
echo $newmessage. "</br></br>";
}
带标签的输出:
不带标签的输出:
错误是由执行顺序引起的,php是级联工作的
foreach ($chatarray as $chat) {
// Replace BBCode-like tags with HTML
$bbcode = array("[b]", "[/b]", "[red]", "[blue]", "[/color]");
$htmlcode = array("<b>", "</b>", "<span style='color:red'>", "<span style='color:blue'>","</span>");
$newmessage = str_replace($bbcode, $htmlcode, $chat);
// Convert newlines to HTML line breaks
$newmessage = nl2br($newmessage);
// Word wrap if necessary
$newmessage = wordwrap($newmessage, 34, "\n", true);
echo $newmessage . "</br></br>";
}
现在在代码中,您首先将 BBCode 标签替换为 HTML 标签
如果这不起作用,则错误也可能出现在您输入的文本中。如果是这样,您可以添加文本如何以及在何处插入吗?