Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How do I replace certain words/text with an image like the way that smilies works?
Old 08-22-2007, 11:56 PM How do I replace certain words/text with an image like the way that smilies works?
Novice Talker

Posts: 7
Trades: 0
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.
Cherriii is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-23-2007, 12:07 AM Re: How do I replace certain words/text with an image like the way that smilies works
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
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.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 08-23-2007, 01:32 AM Re: How do I replace certain words/text with an image like the way that smilies works
Novice Talker

Posts: 7
Trades: 0
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.
Cherriii is offline
Reply With Quote
View Public Profile
 
Old 08-23-2007, 08:51 AM Re: How do I replace certain words/text with an image like the way that smilies works
Skilled Talker

Posts: 94
Trades: 0
Try this

http://us2.php.net/preg_replace
__________________
Sell Templates? Try our
Please login or register to view this content. Registration is FREE
! See a live
Please login or register to view this content. Registration is FREE
ChadR is offline
Reply With Quote
View Public Profile
 
Old 08-23-2007, 09:21 AM Re: How do I replace certain words/text with an image like the way that smilies works
Super Talker

Posts: 134
Trades: 0
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
Falcone is offline
Reply With Quote
View Public Profile
 
Old 08-23-2007, 09:34 AM Re: How do I replace certain words/text with an image like the way that smilies works
Super Talker

Posts: 134
Trades: 0
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
Falcone is offline
Reply With Quote
View Public Profile
 
Old 08-23-2007, 11:47 AM Re: How do I replace certain words/text with an image like the way that smilies works
Novice Talker

Posts: 7
Trades: 0
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?
Cherriii is offline
Reply With Quote
View Public Profile
 
Old 08-23-2007, 12:17 PM Re: How do I replace certain words/text with an image like the way that smilies works
Super Talker

Posts: 134
Trades: 0
here is the link for the smilies system tutorial
http://www.tutorialized.com/view/tut...e-images/27386




__________________

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
Falcone is offline
Reply With Quote
View Public Profile
 
Old 08-23-2007, 12:22 PM Re: How do I replace certain words/text with an image like the way that smilies works
Skilled Talker

Posts: 94
Trades: 0
Quote:
Originally Posted by Cherriii View Post
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.
__________________
Sell Templates? Try our
Please login or register to view this content. Registration is FREE
! See a live
Please login or register to view this content. Registration is FREE
ChadR is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How do I replace certain words/text with an image like the way that smilies works?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22411 seconds with 12 queries