Question

I have this big list of emoticons for the pack that I have downloaded, thankfully they came with the emote codes so I didn't have to make all those (like smile is :) and :-)). Here is an example chunk of the file format it's in.

smile.png           :)      :-)
smile-big.png       :D      :d :-D :-d
wink.png            ;)      ;-)
shock.png           :-O     :-o :O :o
tongue.png          :P      :p :-P :-p
glasses-cool.png    (H)     (h)
angry.png           :@      :-@
embarrassed.png     :$      :-$
confused.png        :S      :s :-S :-s
sad.png             :(      :-(
crying.png          :'(
neutral.png         :|      :-|
devil.png           (6)
angel.png           (A)     (a)
love.png            (L)     (l)
love-over.png       (U)     (u)
msn.png             (M)     (m)
cat.png             (@)
dog.png             (&)
moon.png            (S)
star.png            (*)
film.png            (~)

But, without modifying this list or retyping it in any way, would I be able to go through a string and replace the occurrence of the symbols with the image tag wrapped around the image name. So with that block of emote definitions as a string, is there any function that would work like convert_emotes($text, $emote_definitions); and turn

Hello world :) :D this is a heart emote: (L) and here's a dog (&)

to

Hello world <img src="smile.png" /> <img src="smile-big.png" /> this is a heart emote: <img src="love.png" /> and here's a dog <img src="dog.png" />

Any help is much appreciated!

Was it helpful?

Solution

Not a nice code but it probably works:

<?php
$patterns = array();
$replacements = array();

foreach (split("\n", file_get_contents("file")) as $line){
    $l = preg_split("/\s+/",$line);
    for($i = 1; $i < count($l); $i++){
        $patterns[] = $l[$i];
        $replacements[] = "<img src='{$l[0]}' />";
    }
}


echo str_replace($patterns, $replacements, "Hello world :) :D this is a heart emote: (L) and here's a dog (&)");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top