We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Text area insert html code if certain letters entered.

harry_bela
harry_bela asked
on
Medium Priority
264 Views
Last Modified: 2008-01-09
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.
Comment
Watch Question

what server side scripting are you using for your forum?? ASP, PHP, .NET???
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"))
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
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Super Thanks Andy.

Harry B.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.