I have A LOT of these in php: $var = str_replace( ":fun:"," <img src=\"images/funny.gif\" align=absmiddle border=\"0\" />", $var ); $var = str_replace( ":ang:"," <img src=\"images/angry.gif\" align=absmiddle border=\"0\" />", $var ); PHP: Basically that's the code for the emoticons I have on my site. I need to somehow convert hundreds of lines like that to javascript and html. This is the format: <a href="javascript:emoticon(':fun: ')"><img src="images/funny.gif"></a> <a href="javascript:emoticon(':ang: ')"><img src="images/angry.gif"></a> HTML: It would take a very long time to type it manually, so as a novice php coder, I am looking for a automated alternative. To me this is completely impossible, but maybe someone can help me out? I think it could be done with a complex regex?
Hi! Confusing What U Wanna Do Exactly :S Try This Method: <?php $array = array ( "funny.gif" => ":fun: ", "sad.gif" => ":sad: ", "happy.gif" => ":hap: ", "smile.gif" => ":smi: " ); foreach($array as $key => $value) { ?> <a href="javascript:emoticon('<?php print $value; ?>')"><img src="images/<?php print $key; ?>" align="absmiddle" border="0" height="32" width="32"></a> <br /> <?php } ?> Code (markup):
If you want something fully automatic try this, but firstly you must make sure there aren't other words in that format ( :word: ), if there are you should change the format to something like {:smile:}, anyways here it is: $content=preg_replace('/:(.*?):/i','<a href="javascript:emoticon(':$1: ')"><img src="images/$1.gif"></a>',$content); Code (markup): and make sure the files names are the same as the matching word (ie :smile: - smile.gif)
You don't need to use the i modifier if theirs no alphanumeric data within the expression...but as I can see a XSS vulnerabilitiy? I suggest you change the (.*?) to something like ([a-z0-9\-_]+) and leave the i modifier intact. And also optionally you can combine preg_replace_callback() with file_exists() to make sure its a valid emoticon.
I don't think the provided scripts do what I'm really aiming for. I'll try to clarify. I have over 500 lines of emoticon codes in php that are used for displaying smileys. Example : BEFORE $var = str_replace( ":fun:"," <img src=\"images/funny.gif\" align=absmiddle border=\"0\" />", $var ); $var = str_replace( ":ang:"," <img src=\"images/angry.gif\" align=absmiddle border=\"0\" />", $var ); $var = str_replace( ":ang2:"," <img src=\"images/angry2.gif\" align=absmiddle border=\"0\" />", $var ); $var = str_replace( ":ang2:"," <img src=\"images/angry2.gif\" align=absmiddle border=\"0\" />", $var ); PHP: Note that the :ang: and angry.gif are not the same. The code for the smiley is not the same as the file name. I need something to get the above lines to look like this: AFTER <a href="javascript:emoticon(':fun: ')"><img src="images/funny.gif"></a> <a href="javascript:emoticon(':ang: ')"><img src="images/angry.gif"></a> <a href="javascript:emoticon(':ang2: ')"><img src="images/angry2.gif"></a> <a href="javascript:emoticon(':ang3: ')"><img src="images/angry3.gif"></a> PHP: The :fun: and funny.gif are carried over only. If you are wondering why I need to do this, then it's to display the smileys on a html page, so the user can click on it and the javascript function 'emoticon' can do its thing... I'm starting to think that it's impossible to do it since there are so many lines to be converted.