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.
harry_belaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
harry_belaAuthor Commented:
Super Thanks Andy.

Harry B.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.