How to Insert Smileys in PHP Code?
Asked Answered
E

6

5

I have a Shoutout Box written in PHP language.It doesnt have Smileys Support. How Can I Insert Smiley Support in it?

Elspet answered 4/5, 2010 at 9:54 Comment(0)
B
16

Some PHP that worked for me back in the day ;)

function Smilify(&$subject)
{
    $smilies = array(
        ':|'  => 'mellow',
        ':-|' => 'mellow',
        ':-o' => 'ohmy',
        ':-O' => 'ohmy',
        ':o'  => 'ohmy',
        ':O'  => 'ohmy',
        ';)'  => 'wink',
        ';-)' => 'wink',
        ':p'  => 'tongue',
        ':-p' => 'tongue',
        ':P'  => 'tongue',
        ':-P' => 'tongue',
        ':D'  => 'biggrin',
        ':-D' => 'biggrin',
        '8)'  => 'cool',
        '8-)' => 'cool',
        ':)'  => 'smile',
        ':-)' => 'smile',
        ':('  => 'sad',
        ':-(' => 'sad',
    );

    $sizes = array(
        'biggrin' => 18,
        'cool' => 20,
        'haha' => 20,
        'mellow' => 20,
        'ohmy' => 20,
        'sad' => 20,
        'smile' => 18,
        'tongue' => 20,
        'wink' => 20,
    );

    $replace = array();
    foreach ($smilies as $smiley => $imgName)
    {
        $size = $sizes[$imgName];
        array_push($replace, '<img src="imgs/'.$imgName.'.gif" alt="'.$smiley.'" width="'.$size.'" height="'.$size.'" />');
    }
    $subject = str_replace(array_keys($smilies), $replace, $subject);
}

enter image description here

Bertrand answered 7/4, 2011 at 0:9 Comment(3)
This is one of the best answers I've seen on this site. Massive Thumbs Up.Twig
where can one find free smileys or emoticons to use in own website?Maldives
I know it's been a while, but if anyone else is wondering you can download free Emoticons at this site: findicons.com/search/emoticon And I'm sure there are more for free onlineDenyse
F
7

You can simply do:

<?php
echo str_replace(';)', '<img src="path/to/smile_image.gif" title=";)"/>', $message);
?>
Foist answered 4/5, 2010 at 10:7 Comment(0)
R
2

I found this and it helped me.. http://os.alfajango.com/css-emoticons/

Reproachful answered 30/3, 2015 at 10:10 Comment(0)
A
1

I would use javascript to check added shouts for combinations like ':-)' and replace them with an image of an smiley

Anthropomorphic answered 4/5, 2010 at 9:57 Comment(0)
I
0

Create function with pass string. And replace with the some text to image like below.

function parseString($string ) {
$my_smilies = array(
    ':aln' => '<img src="images/alien1.png" alt="" />',
    ':thk' => '<img src="images/annoyed.png" alt="" />',
    ':ang' => '<img src="images/angel.png" alt="" />',
    ':slp<' => '<img src="images/zzz.png" alt="" />',
    ':blnk' => '<img src="images/blanco.png" alt="" />',
    ':zip' => '<img src="images/zip_it.png" alt="" />',
    ':bor' => '<img src="images/boring.png" alt="" />',

);

return str_replace( array_keys($my_smilies), array_values($my_smilies), $string);

}

Incentive answered 28/10, 2016 at 4:53 Comment(0)
P
0

Better to use smiley codes, no need to use replace functions.

https://www.w3schools.com/charsets/ref_emoji_smileys.asp

eg: <p>I will display &#128512;</p>

result:

I will display 😀

Politician answered 30/6, 2020 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.