Is there a way to use PHP in your chat system to create an emoticon?

I try to parse the text when I saw ":)" I replaced it with the image path, is there any way to do this?

Thanks in advance.

$text = "This would be fun :)"

wanted result = "It would be an interesting way of the image here"

so instead of ":)" I will see the image path to the emoticon

+4
source share
3 answers
$text = "This would be fun :)"
$newtext = str_replace(":)", "<img src='images/smiley.png'>", $text);

for a few replacement examples replace below php.net

$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
// Provides Result: Hll Wrld f PHP
+5
source

Thanks kamlesh

as I read about str_replace obtained for this solution

  $str = "It would be fun :)";
     $image_path_smile = "<img class='emoticons' src='images/smile.png'/>";

    $pattern=array();
    $pattern[0]=":)";

    $replacement=array();
    $replacement[0]=$image_path_smile;

     str_replace($pattern,$replacement,$str);

Thanks for the quick response:)

+2
source

, , , <img>:

<?php
class SmileyReplacer {
    private static $smilies = [':)'=>'happy.png', ';)'=>'blink.png', ':('=>'sad.png'];
    private static $address = 'path/to/file/';

    public static function replace($str) {
        foreach(self::$smilies as $smileyTxt => $smileyFile) {
            $str = str_replace($smileyTxt, "<img src=\"".self::$address.$smileyFile."\"/>", $str);
        }
        return $str;
    }
}

echo SmileyReplacer::replace('Hi :), this would be fun ;) but it\ already over :(');
?>
+1

All Articles