Jump to content

how to make this code more efficient, possibly using arrays?


pouncer

Recommended Posts

Hi guys, i have this function im using but i think its terrible coding..

 

function EmotReplace(text) {
var newText;

newText = text.replace(/:\)/g, '<img src="emoticons/smile.gif">');
newText = newText.replace(/:-\)/g, '<img src="emoticons/smile.gif">');
newText = newText.replace(/<\)/gi, '<img src="emoticons/partyblow.bmp">');
newText = newText.replace(/:O/gi, '<img src="emoticons/surprised.gif">');
newText = newText.replace(/:-O/gi, '<img src="emoticons/surprised.gif">');
newText = newText.replace(/;\)/g, '<img src="emoticons/wink.gif">');
etc etc.. 100's of lines of replacement

 

for e.g, :) would get converted into a smiley emoticon

 

but im doing a replace line for each emoticon i have, which i think is bad coding practise. can someone help me do it a better way, possibly initialising an array with all the emots and text triggers for each emot ??

 

Link to comment
Share on other sites

Any reason why you're not using PHP to do this?

 

I can't really see how this would be useful except if you want to make some routine/tool to help others or yourself HTML in some way.

 

One way is to build a couple arrays...

function emotReplace(txt) {
  var arrFind=array('','','');
  var arrRepl=array('smile.gif','sad.gif','wink');
  newText=txt;
  for (i=0;i<count(arrFind);i++) {
    newText=newText.replace(arrFind[i],'<img src="emoticons/'+arrRepl[i]+'">');
  }
}

That probably won't work but it gives the idea I'm aiming at. I hate Javascript and avoid it like the plague but I had to write my first major script earlier and learnt quite a bit.

Link to comment
Share on other sites

try this:

 

<script language="javascript">
function emotions(display)
{
var currentmsg = document.getElementById('msg').value;
document.getElementById('msg').value=''+currentmsg+' '+display+'';
}
</script>

<div style="margin:10px">

<img src="emoticons/smile.gif" onclick="emotions('')">
<img src="emoticons/partyblow.bmp" onclick="emotions('')">
<img src="emoticons/surprised.gif" onclick="emotions(':0')">
<img src="emoticons/wink.gif" onclick="emotions('')">

</div>

<textarea id="msg" style="width:400px;height:250px;border:solid 1px black"></textarea>

 

you can also for the example that I have previously posted:

 

http://www.phpfreaks.com/forums/index.php/topic,171252.msg758382.html#msg758382

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.