How do I replace certain words/text with an image like the way that smilies works?
08-22-2007, 11:56 PM
|
How do I replace certain words/text with an image like the way that smilies works?
|
Posts: 7
|
I am looking for a script that'll convert certain words/text into an assigned image. For example, when I type the word, cat, I want it displayed as a small image of a cat.
I think this is sort of like how smilies work, where ":-)" is converted into a happy face, ":-(" is converted into a sad face, ":angry:" is converted into an angry face, etc.
I think it is a php script/code, that's why I posted this question in this category. If I've mistaken, please kindly let me know or move the post to an appropriate category.
Any help would be much appreciated. Thanks in advance.
|
|
|
|
08-23-2007, 12:07 AM
|
Re: How do I replace certain words/text with an image like the way that smilies works
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
This is done best by using regular-expression replacement. ereg() or preg_replace() you could check for matches in an array that defines your replacement strings to images.
RegEx tutorial: http://www.regular-expressions.info/
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
08-23-2007, 01:32 AM
|
Re: How do I replace certain words/text with an image like the way that smilies works
|
Posts: 7
|
Thank you, mgraphic
I went to the website that you provided and I ended up being more confused. Sorry to sound like an idiot. It seems there is a lot that you can do with Regular Expression. There are lots of explanation, but no examples...
Is it anything like this:
Code:
function smilies_init() {
global $wpsmiliestrans, $wp_smiliessearch, $wp_smiliesreplace;
// don't bother setting up smilies if they are disabled
if ( !get_option('use_smilies') )
return;
if (!isset($wpsmiliestrans)) {
$wpsmiliestrans = array(
':mrgreen:' => 'icon_mrgreen.gif',
':neutral:' => 'icon_neutral.gif',
':twisted:' => 'icon_twisted.gif',
':arrow:' => 'icon_arrow.gif',
':shock:' => 'icon_eek.gif',
':smile:' => 'icon_smile.gif',
':???:' => 'icon_confused.gif',
':cool:' => 'icon_cool.gif',
':evil:' => 'icon_evil.gif',
':grin:' => 'icon_biggrin.gif',
':idea:' => 'icon_idea.gif',
':oops:' => 'icon_redface.gif',
':razz:' => 'icon_razz.gif',
':roll:' => 'icon_rolleyes.gif',
':wink:' => 'icon_wink.gif',
':cry:' => 'icon_cry.gif',
':eek:' => 'icon_surprised.gif',
':lol:' => 'icon_lol.gif',
':mad:' => 'icon_mad.gif',
':sad:' => 'icon_sad.gif',
'8-)' => 'icon_cool.gif',
'8-O' => 'icon_eek.gif',
':-(' => 'icon_sad.gif',
':-)' => 'icon_smile.gif',
':-?' => 'icon_confused.gif',
':-D' => 'icon_biggrin.gif',
':-P' => 'icon_razz.gif',
':-o' => 'icon_surprised.gif',
':-x' => 'icon_mad.gif',
':-|' => 'icon_neutral.gif',
';-)' => 'icon_wink.gif',
'8)' => 'icon_cool.gif',
'8O' => 'icon_eek.gif',
':(' => 'icon_sad.gif',
':)' => 'icon_smile.gif',
':?' => 'icon_confused.gif',
':D' => 'icon_biggrin.gif',
':P' => 'icon_razz.gif',
':o' => 'icon_surprised.gif',
':x' => 'icon_mad.gif',
':|' => 'icon_neutral.gif',
';)' => 'icon_wink.gif',
':!:' => 'icon_exclaim.gif',
':?:' => 'icon_question.gif',
);
}
$siteurl = get_option('siteurl');
foreach ( (array) $wpsmiliestrans as $smiley => $img ) {
$wp_smiliessearch[] = '/(\s|^)'.preg_quote($smiley, '/').'(\s|$)/';
$smiley_masked = htmlspecialchars(trim($smiley), ENT_QUOTES);
$wp_smiliesreplace[] = " <img src='$siteurl/wp-includes/images/smilies/$img' alt='$smiley_masked' class='wp-smiley' /> ";
}
}
That's from wordpress. The website that I'm working on right now doesn't use Wordpress, though.
|
|
|
|
08-23-2007, 08:51 AM
|
Re: How do I replace certain words/text with an image like the way that smilies works
|
Posts: 94
|
|
|
|
|
08-23-2007, 09:21 AM
|
Re: How do I replace certain words/text with an image like the way that smilies works
|
Posts: 134
|
if you want that the image appears at the moment you type you need a javascript not php
__________________
Please login or register to view this content. Registration is FREE
Check out the Facebook Clone build with Jcow SNS at Please login or register to view this content. Registration is FREE , it is free and it always will be
|
|
|
|
08-23-2007, 09:34 AM
|
Re: How do I replace certain words/text with an image like the way that smilies works
|
Posts: 134
|
if you want to use PHP: i did some googling
use this:
PHP Code:
function smileys($string) {
$smileys = array( ":angry:", ":D", ":blink:", ":blush:", "B)", "<_<", "^_^", ":huh:", ":lol:", ":o", ":fear:", ":rolleyes:", ":(", ":sleep:", ":)", ":p", ":P", ":unsure:", ":wacko:", ":wink:", ":wub:", ); $images = array( "<img src=\"angry.gif\">", "<img src=\"biggrin.gif\">", "<img src=\"blink.gif\">", "<img src=\"blush.gif\">", "<img src=\"cool.gif\">", "<img src=\"dry.gif\">", "<img src=\"happy.gif\">", "<img src=\"huh.gif\">", "<img src=\"laugh.gif\">", "<img src=\"ohmy.gif\">", "<img src=\"fear.gif\">", "<img src=\"rolleyes.gif\">", "<img src=\"sad.gif\">", "<img src=\"sleep.gif\">", "<img src=\"smile.gif\">", "<img src=\"tongue.gif\">", "<img src=\"tongue.gif\">", "<img src=\"unsure.gif\">", "<img src=\"wacko.gif\">", "<img src=\"wink.gif\">", "<img src=\"wub.gif\">", ); return str_replace($smileys, $images, $string); }
__________________
Please login or register to view this content. Registration is FREE
Check out the Facebook Clone build with Jcow SNS at Please login or register to view this content. Registration is FREE , it is free and it always will be
|
|
|
|
08-23-2007, 11:47 AM
|
Re: How do I replace certain words/text with an image like the way that smilies works
|
Posts: 7
|
ChadR:
Thanks for posting the link. I read it. No, I'm not lying. I read it, but I'm too newbie to understand it. Sorry to sound like an idiot (again).
Falcone:
I don't think I'm looking for Javascript, because I don't need it to show up as an image/icon as I type. I just need the "end result" to be displayed with an image/icon/smiley. Sorry for being confusing or vague. I tried Googling, too. I must've done a bad job at it because I couldn't find anything that seems right. But hey, what do I know? =P The codes that you posted look simpler than the WP ones that I jacked from my friend. xD I'm guessing that I should put those codes in a php file, assigned the appropriate/preferred shorthand and url, and then call for it in wherever I want to use it?
|
|
|
|
08-23-2007, 12:17 PM
|
Re: How do I replace certain words/text with an image like the way that smilies works
|
Posts: 134
|
__________________
Please login or register to view this content. Registration is FREE
Check out the Facebook Clone build with Jcow SNS at Please login or register to view this content. Registration is FREE , it is free and it always will be
|
|
|
|
08-23-2007, 12:22 PM
|
Re: How do I replace certain words/text with an image like the way that smilies works
|
Posts: 94
|
Quote:
Originally Posted by Cherriii
ChadR:
Thanks for posting the link. I read it. No, I'm not lying. I read it, but I'm too newbie to understand it. Sorry to sound like an idiot (again).
|
You are going to need to read a book then or take some classes. If you can not understand the PHP manual it is unlikely that anyone here is going to be able to help you without doing the entire code for you. I really enjoyed learning PHP it is a very useful.
|
|
|
|
|
« Reply to How do I replace certain words/text with an image like the way that smilies works?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|