PHP Text to graphical smiley [duplicate]
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);
}
精彩评论