Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP]A way of shortening this huuuge code :P

Joined
Jul 26, 2006
Messages
3,634
Reaction score
1,007
This is the code I use for smileys, and I'd like to shorten it:
PHP:
		$smileys = array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10');
		
		$text = str_replace(":)", "<img src='images/smileys/". $smileys[0] .".gif' />", $text);
		$text = str_replace(":-)", "<img src='images/smileys/". $smileys[0] .".gif' />", $text);
		$text = str_replace("=)", "<img src='images/smileys/". $smileys[0] .".gif' />", $text);
		
		$text = str_replace(":D", "<img src='images/smileys/". $smileys[1] .".gif' />", $text);
		$text = str_replace(":d", "<img src='images/smileys/". $smileys[1] .".gif' />", $text);
		$text = str_replace("=D", "<img src='images/smileys/". $smileys[1] .".gif' />", $text);
		$text = str_replace("=d", "<img src='images/smileys/". $smileys[1] .".gif' />", $text);
		$text = str_replace(":-D", "<img src='images/smileys/". $smileys[1] .".gif' />", $text);
		$text = str_replace(":-d", "<img src='images/smileys/". $smileys[1] .".gif' />", $text);
		
		$text = str_replace(":O", "<img src='images/smileys/". $smileys[2] .".gif' />", $text);
		$text = str_replace(":o", "<img src='images/smileys/". $smileys[2] .".gif' />", $text);
		$text = str_replace("=O", "<img src='images/smileys/". $smileys[2] .".gif' />", $text);
		$text = str_replace("=o", "<img src='images/smileys/". $smileys[2] .".gif' />", $text);
		$text = str_replace(":-O", "<img src='images/smileys/". $smileys[2] .".gif' />", $text);
		$text = str_replace(":-o", "<img src='images/smileys/". $smileys[2] .".gif' />", $text);
		
		$text = str_replace(":p", "<img src='images/smileys/". $smileys[3] .".gif' />", $text);
		$text = str_replace(":P", "<img src='images/smileys/". $smileys[3] .".gif' />", $text);
		$text = str_replace(";p", "<img src='images/smileys/". $smileys[3] .".gif' />", $text);
		$text = str_replace(";P", "<img src='images/smileys/". $smileys[3] .".gif' />", $text);
		$text = str_replace("=p", "<img src='images/smileys/". $smileys[3] .".gif' />", $text);
		$text = str_replace("=P", "<img src='images/smileys/". $smileys[3] .".gif' />", $text);
		$text = str_replace(":-p", "<img src='images/smileys/". $smileys[3] .".gif' />", $text);
		$text = str_replace(":-P", "<img src='images/smileys/". $smileys[3] .".gif' />", $text);
		
		$text = str_replace(":(", "<img src='images/smileys/". $smileys[4] .".gif' />", $text);
		$text = str_replace("=(", "<img src='images/smileys/". $smileys[4] .".gif' />", $text);
		$text = str_replace(":-(", "<img src='images/smileys/". $smileys[4] .".gif' />", $text);
		
		$text = str_replace(":'(", "<img src='images/smileys/". $smileys[5] .".gif' />", $text);
		$text = str_replace("='(", "<img src='images/smileys/". $smileys[5] .".gif' />", $text);
		$text = str_replace(":'-(", "<img src='images/smileys/". $smileys[5] .".gif' />", $text);
		$text = str_replace(":cry:", "<img src='images/smileys/". $smileys[5] .".gif' />", $text);
		
		$text = str_replace(":|", "<img src='images/smileys/". $smileys[6] .".gif' />", $text);
		$text = str_replace(":-|", "<img src='images/smileys/". $smileys[6] .".gif' />", $text);
		
		$text = str_replace(":@", "<img src='images/smileys/". $smileys[7] .".gif' />", $text);
		$text = str_replace(":-@", "<img src='images/smileys/". $smileys[7] .".gif' />", $text);
		
		$text = str_replace("(l)", "<img src='images/smileys/". $smileys[8] .".gif' />", $text);
		$text = str_replace("(L)", "<img src='images/smileys/". $smileys[8] .".gif' />", $text);
		
		$text = str_replace(":police:", "<img src='images/smileys/". $smileys[9] .".gif' />", $text);
		$text = str_replace(":cop:", "<img src='images/smileys/". $smileys[9] .".gif' />", $text);

I've tried something with more arrays but I got confused :p
Any suggestions ?
 
duck you, I'm a dragon
Loyal Member
Joined
Apr 29, 2005
Messages
6,407
Reaction score
130
Holy duck.

First of all, only use 1 command for a smiley instead of 7. Second of all, use preg_replace.

PHP:
function replace_smiley($str) {

$spattern = array(
"/:\)/",
"/:\(/",
"/:(P|p)/"
//etc...
);

$replace = array(
"smileycodehere",
"smileycodehere",
"smileycodehere"
);

$str = preg_replace($spattern, $replace, $str);

return $str;
}

Normally I'd use indentation but I can't use the tab key in this text editor.

Read if you have trouble with the regular expressions used in the search pattern.
 
Experienced Elementalist
Joined
Apr 7, 2005
Messages
254
Reaction score
0
Holy Molly...
That part of code is rather slow, isn't it?

You can use preg_replace as Pieman said, or you can use str_replace with arrays as variables. And i doesn't see any reasons to use preg_replace when you haven't a regexp-patterns :
PHP:
function replaceSmiley($string)
{
$smilies = array(':-)',':-(',':-D');
$replacement = array('codehere','codehere','codehere');
return str_replace($smilies,$replacement,$string);
}
 
Last edited:
duck you, I'm a dragon
Loyal Member
Joined
Apr 29, 2005
Messages
6,407
Reaction score
130
Oh dear, that was a stupid error. :x
 
Back
Top