开发者

PHP Text to graphical smiley [duplicate]

开发者 https://www.devze.com 2023-03-08 09:16 出处:网络
This question already has answers here: Closed开发者_开发知识库 11 years ago. Possible Duplicate:
This question already has answers here: Closed开发者_开发知识库 11 years ago.

Possible Duplicate:

PHP REGEX: Problem with Smiley :) and :))

When always i have a similly in text format in a php page such as ;) :-| :-( can i represent it in graphically ? in all occurrences of the page


Something like this should work:

$smileys = array(
    ':)' => 'smile.gif',
    ':(' => 'sad.gif',
    ':D' => 'happy.gif'
);

$ks = array();
$vs = array();

foreach($smileys as $k => $v){
    $ks[] = $k;
    $vs[] = '<img src="' . $v . '" alt="' . $k . '" />';
}

str_replace($ks, $vs, $output); // presuming that you have the whole page output in $output


You can build a function that converts your smiley to HTML image. Like:

function parseSmiley($text){
    // Smiley to image
    $smileys = array(
        ';)' => 'blink.png',
        ':-|' => 'scare.png',
        ':-(' => 'bad.png'
    );

    // Now you need find and replace
    foreach($smileys as $smiley => $img){
        $text = str_replace(    
            $smiley,
            "<img src='smiley/path/{$img}' />",
            $text
        );
    }

    // Now only return it
    return $text;
}

Now run:

echo parseSmiley('Hello you ;)');


a really easy way to do that, is to replace smiley by a <img> tag

function smileyText($content) {
    $smiley = array(':)', ':(' ...);
    $graph = array('<img src="..."/>', '<img src="..."/>', ...);

    return str_replace($smiley, $graph, $content);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消