Page 1 of 1

[FIXED]Smiley parser with flatfile

Posted: Wed Aug 10, 2005 3:27 pm
by Cronikeys
Code that echos content:

Code: Select all

<? 
$content = ":)  ";
function smiley($content) 
    { 
    $filename = "images/smileys/smileys.txt";
    $lines = file($filename);
    $numlines = count($lines);
   	for($i=0;$i<$numlines;$i++)
        { 
	    $linecol = explode("=+:",$lines[$i]);
            $sm_text[$i] = $linecol[2]; 
            $image[$i] = "<img src=".$linecol[0].">"; 
        }                   
    $content= str_replace($sm_text, $image, $content); 
    return $content; 
    }  
echo smiley($content);
?>
Contents of images/smileys/smileys.txt

Code: Select all

biggrin.gif=+:Very Happy=+::D
biggrin.gif=+:Very Happy=+::-D
biggrin.gif=+:Very Happy=+::grin:
biggrin.gif=+:Very Happy=+::biggrin:
smile.gif=+:Smile=+::)
smile.gif=+:Smile=+::-)
smile.gif=+:Smile=+::smile:
sad.gif=+:Sad=+::(
sad.gif=+:Sad=+::-(
sad.gif=+:Sad=+::sad:
surprised.gif=+:Surprised=+::o
surprised.gif=+:Surprised=+::-o
surprised.gif=+:Surprised=+::eek:
shock.gif=+:Shock=+::shock:
confused.gif=+:Confused=+::?
confused.gif=+:Confused=+::-?
confused.gif=+:Confused=+::???:
cool.gif=+:Cool=+:8)
cool.gif=+:Cool=+:8-)
cool.gif=+:Cool=+::cool:
lol.gif=+:Laughing=+::lol:
mad.gif=+:Mad=+::x
mad.gif=+:Mad=+::-X
mad.gif=+:Mad=+::mad:
razz.gif=+:Razz=+::p
razz.gif=+:Razz=+::-p
razz.gif=+:Razz=+::razz:
redface.gif=+:Embarassed=+::oops:
cry.gif=+:Crying or Very sad=+::cry:
evil.gif=+:Evil or Very Mad=+::evil:
badgrin.gif=+:Bad Grin=+::badgrin:
rolleyes.gif=+:Rolling Eyes=+::roll:
wink.gif=+:Wink=+:;)
wink.gif=+:Wink=+:;-)
wink.gif=+:Wink=+::wink:
exclaim.gif=+:Exclamation=+::!:
question.gif=+:Question=+::?:
idea.gif=+:Idea=+::idea:
arrow.gif=+:Arrow=+::arrow:
neutral.gif=+:Neutral=+::|
neutral.gif=+:Neutral=+::-|
neutral.gif=+:Neutral=+::neutral:
doubt.gif=+:Doubt=+::doubt:
It outputs:

Code: Select all

:) :oops: :o
I have been working on this for so long and I am assuming it is going to end up being a stupid error.

Thanks

Posted: Wed Aug 10, 2005 3:39 pm
by shiznatix
after a quick look at ur code

Code: Select all

$content = str_replace($sm_text, $image, $content); 
return $content;
that makes no sence becuase $content originally has nothing from ur txt file that can be replaced so of course its going to stay the same.

i did NOT take a long look at the code BUT maybe

Code: Select all

$content = str_replace($sm_text, $image, $image[$i]);
instead...

Posted: Wed Aug 10, 2005 3:43 pm
by Cronikeys
I set the content variable at the top with

Code: Select all

$content = ":) :oops: :o";

The php I posted is a shortened version of the actual code, but it is essentially the same. Thanks for the reply though :)

Any other thoughts?

---
EDIT:
Explanation of what the function does:

Sets variable lines to an array of all the lines in the smiley.txt file.
Counts the number of lines in the smiley.txt file.
Split each line into filename, alt and code.
For each line add a smiley code to the sm_text array.
For each line add a image code to the image array.
After done with all the lines replace all the vars in the sm_text array with all the vars in the image array.

Posted: Wed Aug 10, 2005 3:58 pm
by shiznatix

Code: Select all

$sm_text[$i] = $linecol[2];
$image[$i] = "<img src=".$linecol[0].">";
that part is not making any sence to me

what i think it should be in the txt file is instead of the BIG SMILE and whatnot just

smile.gif:::::-smile-

and whatnot then you can explode on the smile.gif and ever other line on the ::::: of those. and if your are using a shoutbox just str_replace('::', ':', $original_string); and if you arn't sharing your source code you should be alright.
________________________________
what i would strongly sugest is that you just hard code this into your script because anyone can put those characters into a submit box and completly mess up your script. just hard code

Code: Select all

$original = str_replace(':-)', '<img src="simle.gif">', $original);
and so on. that is your best bet for sure

Posted: Wed Aug 10, 2005 4:01 pm
by Cronikeys
Thanks for another reply :lol:

The whole point of this code was so I could be lazy and just use the pak file from phpbb.com XD but after 4~ hours of working on this one small function I'd say you are probably right with hardcoding.

However - if anyone can fix my code to work with the pak file PLEASE be my guest as that would probably be an easier route and save space :)

________________________________________
EDIT:
Fixed :)

It's complicated how I fixed it but it does work ;) If you wanna know how IM me at Cronikeys (aim)