/    Sign up×
Community /Pin to ProfileBookmark

How do you Replace text :) with an image :)

Hi,

I would like to simply replace all the emotion text from my database such as : ) and have them replaced with the image ? .

If someone could post a simply change to my code this would help me heeps as I have been looking for one the net for days now,

I simply want the database Field “message” to have the code where all info within that field of the database goes through a filter and finds all the emotions and replaces with images like this forum?,

this is the field code where it is displayed: <? echo $rows[‘message’]; ?>

[code=php]
<?php

// Connect to server and select database.
mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);

$rowsPerPage = 20;
$pageNum = 1;
if(isset($_GET[‘page’]))
{
$pageNum = $_GET[‘page’];
}
$offset = ($pageNum – 1) * $rowsPerPage;

$sql=”SELECT * FROM $tbl_name ORDER BY id DESC LIMIT $offset, $rowsPerPage”;
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){

?>
<table width=”500″ border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td width=”73″><font color=”#FF0000″ size=”1″ face=”Times New Roman, Times, serif”><? echo $rows[‘datetime’]; ?>
-</font></td>
<td colspan=”2″> <font color=”#0000CC” size=”3″ face=”Times New Roman, Times, serif”><? echo $rows[‘TippingName’]; ?></font></td>
</tr>
<tr>
<td colspan=”2″ align=”center” valign=”top”><font size=”2″ face=”Times New Roman, Times, serif”>Message:</font></td>
<td width=”426″ align=”left” valign=”top”><font color=”#000000″ size=”2″ face=”Times New Roman, Times, serif”>
[B]<? echo $rows[‘message’]; ?>[/B]</font></td>
</tr>
</table>
<div align=”center”></div>
<hr align=”center” width=”450″ size=”1″ color=”#999999″>
<center><font color=”#000000″ size=”2″><?
}

$query = “SELECT COUNT(id) AS numrows FROM $tbl_name”;
$result = mysql_query($query) or die(‘Error, query failed’);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row[‘numrows’];
$maxPage = ceil($numrows/$rowsPerPage);
$self = $_SERVER[‘PHP_SELF’];
if ($pageNum > 1)
{
$page = $pageNum – 1;
$prev = ” <a href=”$self?affc=msgboard&page=$page”><font color=#FF0000>[&laquo;]</font></a> “;
$first = ” <a href=”$self?affc=msgboard&page=1″><font color=#FF0000>[&laquo;&laquo;]</font></a> “;
}
else
{
$prev = ‘ [&laquo;] ‘;
$first = ‘ [&laquo;&laquo;] ‘;
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = ” <a href=”$self?affc=msgboard&page=$page”><font color=#FF0000>[&raquo;]</font></a> “;
$last = ” <a href=”$self?affc=msgboard&page=$maxPage”><font color=#FF0000>[&raquo;&raquo;]</font></a> “;
}
else
{
$next = ‘ [&raquo;] ‘;
$last = ‘ [&raquo;&raquo;] ‘;
}
echo “$first”. $prev . ” Page <strong>$pageNum</strong> of <strong>$maxPage</strong> ” . $next . $last;
mysql_close();
?>
[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 03.2008 — [code=php]
$emoticons = array(
'#b:)b#' => '<img src="images/smile.gif" alt="">',
'#b:(b#' => '<img src="images/frown.gif" alt="">'
);
$text = preg_replace(array_keys($emoticons), $emoticons, $text);
[/code]
Copy linkTweet thisAlerts:
@andre4s_yFeb 03.2008 — Continue NogDog's work, with no array :
[code=php]
<?php
while($rows=mysql_fetch_array($result)){
$comment = $rows['message'];
//standard smilies in webdeveloper
$comment = preg_replace("/:)/","<img title="Smilie" alt="smilie" src="http://www.webdeveloper.com/forum/images/smilies/smile.gif" />",$comment);// :)
$comment = preg_replace("/:(/","<img title="Frown" alt="frown" src="http://www.webdeveloper.com/forum/images/smilies/frown.gif" />",$comment);// :(
$comment = preg_replace("/:o/i","<img title="Embarrassment" alt="redface" src="http://www.webdeveloper.com/forum/images/smilies/redface.gif" />",$comment);// :o
$comment = preg_replace("/:D/i","<img title="Big Grin" alt="biggrin" src="http://www.webdeveloper.com/forum/images/smilies/biggrin.gif" />",$comment);// :D
$comment = preg_replace("/;)/","<img title="Wink" alt="wink" src="http://www.webdeveloper.com/forum/images/smilies/wink.gif" />",$comment);// ;)
$comment = preg_replace("/:p/i","<img title="Stick Out Tongue" alt="tongue" src="http://www.webdeveloper.com/forum/images/smilies/tongue.gif" />",$comment);// :P
$comment = preg_replace("/:cool:/i","<img title="Cool" alt="cool" src="http://www.webdeveloper.com/forum/images/smilies/cool.gif" />",$comment);// :cool:
$comment = preg_replace("/:rolleyes:/","<img title="Roll Eyes" alt="rolleyes" src="http://www.webdeveloper.com/forum/images/smilies/rolleyes.gif" />",$comment);// :rolleyes:
$comment = preg_replace("/:mad:/","<img title="Mad" alt="mad" src="http://www.webdeveloper.com/forum/images/smilies/mad.gif" />",$comment);// :mad:
$comment = preg_replace("/:eek:/","<img title="EEK!" alt="eek" src="http://www.webdeveloper.com/forum/images/smilies/eek.gif" />",$comment);// :eek:
$comment = preg_replace("/:confused:/","<img title="Confused" alt="confused" src="http://www.webdeveloper.com/forum/images/smilies/confused.gif" />",$comment);// :confused:
?>
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="73"><font color="#FF0000" size="1" face="Times New Roman, Times, serif"><? echo $rows['datetime']; ?>
-</font></td>
<td colspan="2"> <font color="#0000CC" size="3" face="Times New Roman, Times, serif"><? echo $rows['TippingName']; ?></font></td>
</tr>
<tr>
<td colspan="2" align="center" valign="top"><font size="2" face="Times New Roman, Times, serif">Message:</font></td>
<td width="426" align="left" valign="top"><font color="#000000" size="2" face="Times New Roman, Times, serif">
[b]<? echo $comment; ?>[/b]</font></td>
</tr>
</table>
<div align="center"></div>
<hr align="center" width="450" size="1" color="#999999">
<center><font color="#000000" size="2">
[/code]
Copy linkTweet thisAlerts:
@airforcefcauthorFeb 03.2008 — Awesome, thanx andre4s_y and NogDog for the replies.

andre4s_y your script was exactly what I was after, you made it very easy to understand and works a charm with my site, thank you so much.

Is there any easy scripts like the one you gave to make my text field work better to put in these smilies as I just have them on the page with the ? next to the image and the user manually puts it into the text box. I would love to have a Popup window with those smilies you gave me in it and then the user just clicks on the image and the script closes the window and places the smilie text in the text box which goes into the database when submitted.

Something like this but I need help with the actual code and the info in the popup window.

[code=php]
<a href="window.popup">
<img src="http://www.webdeveloper.com/forum/images/smilies/smile.gif">
</a>
<input name="message" type="text" id="message" size="68" maxlength="200">[/code]
Copy linkTweet thisAlerts:
@NogDogFeb 03.2008 — You might want to consider something like [url=http://tinymce.moxiecode.com/]TinyMCE[/url], which has an [url=http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/emotions]"emotions" plugin[/url] for smilies.
Copy linkTweet thisAlerts:
@airforcefcauthorFeb 03.2008 — NogDog

Thanx heeps, I can use this code for so much more than just emotions as well, easy to install and works a treat, thanx for all your help in getting me up n running with emotions.
Copy linkTweet thisAlerts:
@andre4s_yFeb 03.2008 — Maybe this simple code answer all :

Main page that contain form input => with id="message"
[code=html]
<html><head>
<script type="text/javascript">
function fillsmiley(text)
{
//IE support
var myField = document.getElementById('message');
var myValue=text;
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)+myValue myField.value.substring(endPos, myField.value.length);
}
else {
myField.value += myValue;
}
}
function openmenu()
{
window.open('smiley.html','','width=270,height=100,resizable=no');
}
</script>
</head>
<body>
<table>
<tr style="text-align:center">
<td><img onclick="fillsmiley(':)')" class="button" title="Smilie" alt="smilie" src="http://www.webdeveloper.com/forum/images/smilies/smile.gif" /></td>
<td><img onclick="fillsmiley(':(')" class="button" title="Frown" alt="frown" src="http://www.webdeveloper.com/forum/images/smilies/frown.gif" /></td>
<td><img onclick="fillsmiley(':D')" class="button" title="Big Grin" alt="biggrin" src="http://www.webdeveloper.com/forum/images/smilies/biggrin.gif" /></td>
<td><img onclick="fillsmiley(';)')" class="button" title="Wink" alt="wink" src="http://www.webdeveloper.com/forum/images/smilies/wink.gif" /></td>
<td><img onclick="fillsmiley(':P')" class="button" title="Stick Out Tongue" alt="tongue" src="http://www.webdeveloper.com/forum/images/smilies/tongue.gif" /><a href="javascript:openmenu()">All</a></td>
</tr>
</table>
<form>
<input name="message" type="text" id="message" size="68" maxlength="200" />
</form>
</body>
</html>
[/code]

Create other page, for example smiley.html :
[code=html]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Standar Smiley</title>
<style type="text/css">
.button {cursor:pointer; background-color:white; border:1px solid white;}
.button:hover {cursor:pointer; background-color:white; border-top:1px solid #FAFAFA; border-left:1px solid #FAFAFA; border-right:1px solid #C8C8C8; border-bottom:1px solid #C8C8C8;}
.button:active {cursor:pointer; background-color:white; border-top:1px solid #C8C8C8; border-left:1px solid #C8C8C8; border-right:1px solid #FAFAFA; border-bottom:1px solid #FAFAFA;}
</style>
<script type="text/javascript">
function fillsmiley(text)
{
//IE support
var myField = window.opener.document.getElementById('message');
var myValue=text;
if (window.opener.document.selection) {
myField.focus();
sel = window.opener.document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)+myValue+myField.value.substring(endPos, myField.value.length);
}
else {
myField.value += myValue;
}
}
</script>
</head>
<body>
<table width="200px" border="0" style="margin-left:auto;margin-right:auto;">
<tr style="text-align:center"><td colspan="11">Standar Smiley</td></tr>
<tr style="text-align:center">
<td><img onclick="fillsmiley(':)')" class="button" title="Smilie" alt="smilie" src="http://www.webdeveloper.com/forum/images/smilies/smile.gif" /></td>
<td><img onclick="fillsmiley(':(')" class="button" title="Frown" alt="frown" src="http://www.webdeveloper.com/forum/images/smilies/frown.gif" /></td>
<td><img onclick="fillsmiley(':o')" class="button" title="Embarrassment" alt="redface" src="http://www.webdeveloper.com/forum/images/smilies/redface.gif" /></td>
<td><img onclick="fillsmiley(':D')" class="button" title="Big Grin" alt="biggrin" src="http://www.webdeveloper.com/forum/images/smilies/biggrin.gif" /></td>
<td><img onclick="fillsmiley(';)')" class="button" title="Wink" alt="wink" src="http://www.webdeveloper.com/forum/images/smilies/wink.gif" /></td>
<td><img onclick="fillsmiley(':P')" class="button" title="Stick Out Tongue" alt="tongue" src="http://www.webdeveloper.com/forum/images/smilies/tongue.gif" /></td>
<td><img onclick="fillsmiley(':cool:')" class="button" title="Cool" alt="cool" src="http://www.webdeveloper.com/forum/images/smilies/cool.gif" /></td>
<td><img onclick="fillsmiley(':rolleyes:')" class="button" title="Roll Eyes" alt="rolleyes" src="http://www.webdeveloper.com/forum/images/smilies/rolleyes.gif" /></td>
<td><img onclick="fillsmiley(':mad:')" class="button" title="Mad" alt="mad" src="http://www.webdeveloper.com/forum/images/smilies/mad.gif" /></td>
<td><img onclick="fillsmiley(':eek:')" class="button" title="EEK!" alt="eek" src="http://www.webdeveloper.com/forum/images/smilies/eek.gif" /></td>
<td><img onclick="fillsmiley(':confused:')" class="button" title="Confused" alt="confused" src="http://www.webdeveloper.com/forum/images/smilies/confused.gif" /></td>
</tr>
</table>
</body>
[/code]


Across the boundary of PHP..

But still web... :p
×

Success!

Help @airforcefc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...