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
https://forums.phpfreaks.com/topic/79065-parsing-smilies/
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
https://forums.phpfreaks.com/topic/79065-parsing-smilies/#findComment-400138
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
https://forums.phpfreaks.com/topic/79065-parsing-smilies/#findComment-400142
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
https://forums.phpfreaks.com/topic/79065-parsing-smilies/#findComment-400167
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.