Simple Smiley Script

Hi, everyone… Well I usually don’t post here on Sitepoint but I do read the articles and co… I made this simple PHP Smiley Script and I need help getting it to work… I have tried everyhting I can and it doesn’t work.

No errors or anything of such… it just doen’t work. The idea is to replace :slight_smile: with a real smiley image and it doen’t do that… Here is the code… Please help.



<!--
Name: Empire X Smiley Script
URL: http://www.empirex.net
Code: <? include_once ("smileys.php"); ?>
By: Empire X Development Team
Coder: Olajide Olaolorun [empirex.net]
-->

<?
function parseEmotions ($text) {
$emoticons = array();
$emoticons[] = array(":)","<img src='smile.gif' />");
$emoticons[] = array(":(","<img src='sad.gif' />");
$emoticons[] = array(":D","<img src='biggrin.gif' />");
$emoticons[] = array("oO","<img src='blink.gif' />");
$emoticons[] = array(":!","<img src='excl.gif' />");
$emoticons[] = array(":)","<img src='smile.gif' />");
$emoticons[] = array(":love","<img src='wub.gif' />");
$emoticons[] = array(":wink","<img src='wink.gif' />");
$emoticons[] = array(":mad","<img src='angry.gif' />");
$emoticons[] = array(":blush","<img src='blush.gif' />");
$emoticons[] = array(":cool","<img src='cool.gif' />");
$emoticons[] = array(":dry","<img src='dry.gif' />");
$emoticons[] = array(":happy","<img src='happy.gif' />");
$emoticons[] = array(":huh","<img src='huh.gif' />");
$emoticons[] = array(":laugh","<img src='laugh.gif' />");
$emoticons[] = array(":oh","<img src='ohmy.gif' />");
$emoticons[] = array(":tongue","<img src='tongue.gif' />");
foreach ($emoticon as $emoticon) {
$text = str_replace($emoticon[0],$emoticon[1]);
}
return $text;
}
?>


:)


Also to ask, would any one have a code beutifier like the one on this forum that highlights PHP code…

fix this bit,


foreach ($emoticons as $emoticon) { // the first should be $emoticons with an s

In your foreach loop the first variable $emoticon should be $emoticons.

Guess I was a little slow on hitting the submit button… :slight_smile:

It still doesn’t work.

The new code is:



<?

/*
Name: Empire X Smiley Script
URL: http://www.empirex.net
Code: <? include_once ("smileys.php"); ?>
By: Empire X Development Team
*/

function parseEmotions ($text) {
$emoticons = array();

$emoticons[] = array(":)","<img src='smile.gif'");
$emoticons[] = array(":(","<img src='sad.gif'");
$emoticons[] = array(":D","<img src='biggrin.gif'");
$emoticons[] = array("oO","<img src='blink.gif'");
$emoticons[] = array(":!","<img src='excl.gif'");
$emoticons[] = array(":)","<img src='smile.gif'");
$emoticons[] = array(":love","<img src='wub.gif'");
$emoticons[] = array(":wink","<img src='wink.gif'");
$emoticons[] = array(":mad","<img src='angry.gif'");
$emoticons[] = array(":blush","<img src='blush.gif'");
$emoticons[] = array(":cool","<img src='cool.gif'");
$emoticons[] = array(":dry","<img src='dry.gif'");
$emoticons[] = array(":happy","<img src='happy.gif'");
$emoticons[] = array(":huh","<img src='huh.gif'");
$emoticons[] = array(":laugh","<img src='laugh.gif'");
$emoticons[] = array(":oh","<img src='ohmy.gif'");
$emoticons[] = array(":tongue","<img src='tongue.gif'");

foreach ($emoticons as $emoticon) {
$text = str_replace($emotion[0],$emotion[1]);
}
return $text;
}
?>

:)


str_replace can also process whole arrays at once, no need of loops. Try this


// define icons
// smiley text is array key
// image is a value

$emoticons = array(
  ":)"  => "<img src='smile.gif'>",
  ":("  => "<img src='sad.gif'>",
// etc

);

return str_replace(
   array_keys($emoticons),
   $emoticons,
   $text);

Well it still doen’t work. This is the new code:



<?

/*
Name: Empire X Smiley Script
URL: http://www.empirex.net
Code: <? include_once ("smileys.php"); ?>
By: Empire X Development Team
*/


$emoticons = array(
  ":)"  => "<img src='http://www.empirex.net/smileys/smile.gif'>",
  ":("  => "<img src='http://www.empirex.net/smileys/sad.gif'>",
  ":D"  => "<img src='http://www.empirex.net/smileys/biggrin.gif'>",
  "oO"  => "<img src='http://www.empirex.net/smileys/blink.gif'>",
  ":!"  => "<img src='http://www.empirex.net/smileys/excl.gif'>",
  ":)"  => "<img src='http://www.empirex.net/smileys/smile.gif'>",
  ":love"  => "<img src='http://www.empirex.net/smileys/wub.gif'>",
  ":wink"  => "<img src='http://www.empirex.net/smileys/wink.gif'>",
  ":mad"  => "<img src='http://www.empirex.net/smileys/angry.gif'>",
  ":blush"  => "<img src='http://www.empirex.net/smileys/blush.gif'>",
  ":cool"  => "<img src='http://www.empirex.net/smileys/cool.gif'>",
  ":dry"  => "<img src='http://www.empirex.net/smileys/dry.gif'>",
  ":happy"  => "<img src='http://www.empirex.net/smileys/happy.gif'>",
  ":huh"  => "<img src='http://www.empirex.net/smileys/huh.gif'>",
  ":laugh"  => "<img src='http://www.empirex.net/smileys/laugh.gif'>",
  ":oh"  => "<img src='http://www.empirex.net/smileys/ohmy.gif'>",
  ":tongue"  => "<img src='http://www.empirex.net/smileys/tongue.gif'>",

);

return str_replace(
   array_keys($emoticons),
   $emoticons,
   $text);

?>

:) - Test Smiley


The link is here: http://www.empirex.net/smileys/smileys.php

[/QUOTE]on the last line, try:

```php

return str_replace(array_keys($emoticons),array_values($emoticons),$text);

it still doesn’t work… ?

I even made a few chnages to the end line and tried it all over again:



<?

/*
Name: Empire X Smiley Script
URL: http://www.empirex.net
Code: <? include_once ("smileys.php"); ?>
By: Empire X Development Team
*/


$emoticons = array(
  ":)"  => "<img src='http://www.empirex.net/smileys/smile.gif'>",
  ":("  => "<img src='http://www.empirex.net/smileys/sad.gif'>",
  ":D"  => "<img src='http://www.empirex.net/smileys/biggrin.gif'>",
  "oO"  => "<img src='http://www.empirex.net/smileys/blink.gif'>",
  ":!"  => "<img src='http://www.empirex.net/smileys/excl.gif'>",
  ":)"  => "<img src='http://www.empirex.net/smileys/smile.gif'>",
  ":love"  => "<img src='http://www.empirex.net/smileys/wub.gif'>",
  ":wink"  => "<img src='http://www.empirex.net/smileys/wink.gif'>",
  ":mad"  => "<img src='http://www.empirex.net/smileys/angry.gif'>",
  ":blush"  => "<img src='http://www.empirex.net/smileys/blush.gif'>",
  ":cool"  => "<img src='http://www.empirex.net/smileys/cool.gif'>",
  ":dry"  => "<img src='http://www.empirex.net/smileys/dry.gif'>",
  ":happy"  => "<img src='http://www.empirex.net/smileys/happy.gif'>",
  ":huh"  => "<img src='http://www.empirex.net/smileys/huh.gif'>",
  ":laugh"  => "<img src='http://www.empirex.net/smileys/laugh.gif'>",
  ":oh"  => "<img src='http://www.empirex.net/smileys/ohmy.gif'>",
  ":tongue"  => "<img src='http://www.empirex.net/smileys/tongue.gif'>",

);

return str_replace(array_keys($emoticons),array_values($emoticons),$text);

?>

:) - Test Smiley


Is there any reason that you have the smile “:)” in the array twice?

I already chnaged that, but that isn’t the problem…

Might want to try echoing the results :wink:

how?

damn, can anyone please help!

Just realized I’ve posted incomplete code, which led to confusion. empirex , your initial idea to write a function was correct. Just put it all together:


/*

your majestic copyright statement here ;)

*/

function parseEmotions ($text)
{
   $emoticons = array(
      ":)"  => "<img src='http://www.empirex.net/smileys/smile.gif'>",

// etc

);
   return str_replace(
     array_keys($emoticons),
     $emoticons,
     $text);
}

$yourMessage = "hello :) world :(";
echo parseEmotions ($yourMessage);

The thing is that I don’t want to be parsing it everytime. I just want to include it in any page and then it turns my :slight_smile: to a real image. I don’t want to echo it or anything like that, just include the code and then it turns it to the real thing.

Please help and thanks for your help stereofrog… :slight_smile:

Just run the imputed text through that function before storing it the database. Then the text in the database will be stored the <img> tags. Or, probably a better option would be to store the original text in the db and then run it through the function before displaying it, that way you aren’t storing html in the db.

What in the world are you talking about… :slight_smile: what do you mean text in database and img… the text is in a file and I just want to include the script to the file via PHP include. When it is included I expect it to run and change this : ) to a real image.

database, file it is a semantic difference. I made the assumption (yes I know what “they” say about assumptions, but you never mentioned where you were getting the text) that text would be entered into a field and stored somewhere (which I guessed would be a database).

Where exactly is the text coming from? Is it prexisting? What kind of file is it in?

I don’t want to echo it or anything like that, just include the code and then it turns it to the real thing.
This really doesn’t make any sense, sorry.

Maybe someone else can explain it better.