Jump to content

Parsing smilies


jaymc

Recommended Posts

Is this the most practical way of parsing smiley code

 

, in other words, turn this

 

; ) into ;)

 

Here is my code, there is 90 lines of this, obvious you dont need all

 

$data[message] = str_replace(":ahhhh:", "<img src=incl/livechatsmiles/ahhhh.gif>", $data[message]);
$data[message] = str_replace(":alien:", "<img src=incl/livechatsmiles/alien.gif>", $data[message]);
$data[message] = str_replace(":@", "<img src=incl/livechatsmiles/cursing.png>", $data[message]);
$data[message] = str_replace(":arse:", "<img src=incl/livechatsmiles/arse.gif>", $data[message]);
$data[message] = str_replace(":bear:", "<img src=incl/livechatsmiles/bear.gif>", $data[message]);
$data[message] = str_replace("", "<img src=incl/livechatsmiles/birthday.gif>", $data[message]);
$data[message] = str_replace(":bk:", "<img src=incl/livechatsmiles/bk.gif>", $data[message]);
$data[message] = str_replace("", "<img src=incl/livechatsmiles/blink.png>", $data[message]);
$data[message] = str_replace(":blush:", "<img src=incl/livechatsmiles/blush.png>", $data[message]);
$data[message] = str_replace(":bmw:", "<img src=incl/livechatsmiles/bmw.gif>", $data[message]);
$data[message] = str_replace(":bop:", "<img src=incl/livechatsmiles/bop.gif>", $data[message]);
$data[message] = str_replace(":bored:", "<img src=incl/livechatsmiles/bored.png>", $data[message]);
$data[message] = str_replace(":boxing:", "<img src=incl/livechatsmiles/boxing.gif>", $data[message]);
$data[message] = str_replace(":boykiss:", "<img src=incl/livechatsmiles/boykiss.gif>", $data[message]);
$data[message] = str_replace(":bye:", "<img src=incl/livechatsmiles/bye.gif>", $data[message]);

 

I have the same approach in javascript, using varname.replace

 

Just wondering, is there a better, more efficient way to do this? Needs to be very optimal

Link to comment
Share on other sites

Rather than calling str_replace over and over, make an array out of your search terms, and an array out of your replace terms. str_replace accepts an array for both. eg;

 

<?php

  $search = array('a','b');
  $replace = array('x','y');
  echo str_replace($search,$replace,'this is a string with big array replacements');

?>

Link to comment
Share on other sites

In this example

 

$search = array('a','b');

  $replace = array('x','y');

 

Would a become x and b become y

 

If so, I guess its based on order in the array

 

search key 21 is replace by replace key 21 for example?

 

 

If this is the case, this wont actually speed things up, just organise the code better...?

Link to comment
Share on other sites

Yes it replaces in order. ie; $search[0] is rplaced by $replace[0] and so on.

 

Your code really is quite efficient, however I would suggest that a function call costs time. Placing your values into an array and only calling the function once will likely be more efficient. The difference however will be completely negligible.

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.