jaymc Posted November 27, 2007 Share Posted November 27, 2007 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 Quote Link to comment Share on other sites More sharing options...
trq Posted November 27, 2007 Share Posted November 27, 2007 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'); ?> Quote Link to comment Share on other sites More sharing options...
jaymc Posted November 27, 2007 Author Share Posted November 27, 2007 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...? Quote Link to comment Share on other sites More sharing options...
trq Posted November 27, 2007 Share Posted November 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
jaymc Posted November 27, 2007 Author Share Posted November 27, 2007 Ok sounds great, I will make amendments Any way to do the same with javascripts split? Or does it too allow for the same approach as ilustrated above Cheers Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.