str_用图像替换文本 [英] str_replace text with image

查看:29
本文介绍了str_用图像替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个聊天框,想用一个图标替换诸如:D"之类的东西.

I have a chat box and would like to replace things such as ' :D ' with an icon.

$chatText = str_replace(":D","<img src='images/icons/smileys/smile-big.png' width='20' height='20' alt='Big Smile' />",$chatText);

以上是我正在使用的代码.在聊天中,不是用情感代替文字,而是直接用 html 代码代替:D":

Above is the code I'm using. On the chat, instead of replacing text with emotions, it literally replaces the ":D" with the html code:

<img src='images/icons/smileys/smile-big.png' width='20' height='20' alt='Big Smile' />

我已经尝试了一些更改(我已经研究过),例如:

I have made a few attempts at changes (that I've researched) such as:

$chatText = str_replace(":D", "< img src=\"/images/icons/smileys/smile-big.png\" width=\"20\" height=\"20\" alt=\"Big Smile\" />",$chatText);

我在那里不走运.我的问题是我怎样才能让它发挥作用,让用户能够有情绪.在我的代码的最顶部,我还有 $chatText = htmlspecialchars($chatText, ENT_QUOTES);

I've had no luck there. My question is how can I get this to work, so users are able to have emotions. At the very top of my code I also have $chatText = htmlspecialchars($chatText, ENT_QUOTES);

推荐答案

我看到了几个可能的问题——从你的代码中不是 100% 清楚:

I see a couple of possible problems — not 100% clear from your code:

如果您只是为 textarea 设置文本,那么它不支持嵌套 HTML,并且您将无法以这种方式制作表情符号.像 gmail 和 facebook 这样支持这些的东西,基本上已经重新实现了内容的呈现.

If you're just setting the text for a textarea, then it doesn't support nested HTML, and you're not going to be able to do the smilies this way. Things like gmail and facebook, that DO support these, have basically re-implemented rendering of the content.

要正确设置文本,您需要使用某些特定 div 元素的 innerHTML 属性来正确设置 HTML.

To correctly set the text, you want to use the innerHTML property of some particular div element to get the HTML correctly set.

在 JQuery 中我可能会写:

In JQuery I'd probably write:

$('chat_text').html($('chat_text').html() + fixed_up_smilie_text);

可能性 #2:您正在使用 htmlspecialchars

如果在将文本放入 HTML 之前使用 htmlspecialchars 对文本进行转义,那么您将看到上面看到的内容.解决方法很简单:不要^_^.

Possibility #2: You're using htmlspecialchars

If you escape the text using htmlspecialchars before putting it in the HTML, then you'll see what you're seeing above. The fix is easy: Don't ^_^.

这篇关于str_用图像替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆