• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Text area insert html code if certain letters entered.

Hi.

I want to setup a really simple forum type post script and am a bit stuck on the following.

I have a 1 x textarea on and 1 x button

What to happen is when the users enters text and then special pre-defined letter arragments like :-) or :( (smilies basically) on submit the letter arragments is repalced with html code (effectively a find and replace) which will show the smilie image associated with that text.

So the user enters

This is just a test :-)

on submit would become

This is just a test <img src="./images/smilie1.gif">

or

This is just a test ;-)

= This is just a test <img src="./images/smilie8.gif">

etc... etc...

Their may be lots of smilies so i need to be able to enter multiple variables in the function.

Can anyone help and post the complete working proof of concept code?

Many thanks

Harry.
0
harry_bela
Asked:
harry_bela
  • 3
1 Solution
 
REA_ANDREWCommented:
what server side scripting are you using for your forum?? ASP, PHP, .NET???
0
 
REA_ANDREWCommented:
if ASP then here is a function

function CahngeSmile(what)
dim smiles
smiles = Array(":-)",":(",":)",":-(",":-p",":-O")
for i = 0 to Ubound(smiles)
select case i
case 0:
Replace(what,smiles(i),"<img src='./images/smilie1.gif'>")
case 1:
Replace(what,smiles(i),"<img src='./images/smilie2.gif'>")
case 2:
Replace(what,smiles(i),"<img src='./images/smilie3.gif'>")
case 3:
Replace(what,smiles(i),"<img src='./images/smilie4.gif'>")
case 4:
Replace(what,smiles(i),"<img src='./images/smilie5.gif'>")
case 5:
Replace(what,smiles(i),"<img src='./images/smilie6.gif'>")
end select
next
end function

then to action this all you do is this. Assuming your server side scripting is asp then when you retrieve the text from the user's submited post, just place it is like this

dim userData
userData = CahngeSmile(request.form("YourTextAreaName"))
0
 
REA_ANDREWCommented:
lol missed out the vital part. Here it is corrected. Just a suggestion

function CahngeSmile(what)
dim smiles
smiles = Array(":-)",":(",":)",":-(",":-p",":-O")
for i = 0 to Ubound(smiles)
select case i
case 0:
Replace(what,smiles(i),"<img src='./images/smilie1.gif'>")
case 1:
Replace(what,smiles(i),"<img src='./images/smilie2.gif'>")
case 2:
Replace(what,smiles(i),"<img src='./images/smilie3.gif'>")
case 3:
Replace(what,smiles(i),"<img src='./images/smilie4.gif'>")
case 4:
Replace(what,smiles(i),"<img src='./images/smilie5.gif'>")
case 5:
Replace(what,smiles(i),"<img src='./images/smilie6.gif'>")
end select
next
CahngeSmile = what
end function
0
 
AndrewShanklinCommented:
Harry,

This is the solution:

<SCRIPT LANGUAGE="JavaScript"><!--
function replace(string,text,by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;
    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;
    var newstr = string.substring(0,i) + by;
    if (i+txtLength < strLength)
    newstr += replace(string.substring(i+txtLength,strLength),text,by);
    return newstr;
}

function Replace() {
    text = document.Post.output.value;
    text = replace(text,':-)',unescape('<img src="./images/smilie.gif">'));
    text = replace(text,':-(',unescape('<img src="./images/smilie1.gif">'));
    text = replace(text,';-(',unescape('<img src="./images/smilie2.gif">'));
    text = replace(text,':-p',unescape('<img src="./images/smilie3.gif">'));
    text = replace(text,'=-0',unescape('<img src="./images/smilie4.gif">'));
    text = replace(text,':-*',unescape('<img src="./images/smilie5.gif">'));
    text = replace(text,'>:0',unescape('<img src="./images/smilie6.gif">'));
    text = replace(text,'0:-)',unescape('<img src="./images/smilie7.gif">'));
    document.Post.output.value = text;
}
</SCRIPT>


<FORM NAME="Post">
<TEXTAREA Name="output" rows="20" cols="50"></TEXTAREA>
<INPUT TYPE="BUTTON" VALUE="Submit" onclick="Replace()">
</FORM>

Andy.

0
 
harry_belaAuthor Commented:
Super Thanks Andy.

Harry B.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now