PDA

View Full Version : :-) into SMileys


Comglomo
08-25-2003, 08:10 PM
Ok.. anyways..
I want when you type in a :-).. :-P :-D those kinda things on my site.. it turns them into images like :) :(
and that stuff.. Like BB Code. Anyone help

Nightfire
08-25-2003, 08:27 PM
Try

$str = preg_replace("/:-)/","<img src='smiley.gif'>",$str);

Spookster
08-25-2003, 08:29 PM
That just a matter of parsing the user entered strings and replacing them with the appropriate image tag.

Easiest way to write code for this is to not reinvent the wheel. Most BBs have code for doing that. Just download one of the major free ones and check out the section of code for doing that

Other than that you learn to do it yourself using PHPs string functions. For this you could use:

http://us2.php.net/manual/en/function.str-replace.php

Comglomo
08-26-2003, 03:16 AM
Hmm

<?php
$str = preg_replace("/:)/","<img src='happy.gif'>",$str);
$str = preg_replace("/:-)/","<img src='happy.gif'>",$str);

$str = preg_replace("/:(/","<img src='smilies/sad.gif'>",$str);
$str = preg_replace("/:-(/","<img src='smilies/sad.gif'>",$str);

$str = preg_replace("/:O/","<img src='smilies/shocked.gif'>",$str);
$str = preg_replace("/:-O/","<img src='smilies/shocked.gif'>",$str);

$str = preg_replace("/:D/","<img src='smilies/biggrin.gif'>",$str);
$str = preg_replace("/:-D/","<img src='smilies/biggrin.gif'>",$str);

$str = preg_replace("/;)/","<img src='smilies/wink.gif'>",$str);
$str = preg_replace("/;-)/","<img src='smilies/wink.gif'>",$str);

$str = preg_replace("/:P/","<img src='smilies/tongue.gif'>",$str);
$str = preg_replace("/:-P/","<img src='smilies/tongue.gif'>",$str);

$str = preg_replace(":bowling:","<img src='smilies/bowl.gif'>",$str);
$str = preg_replace(":mad:/","<img src='smilies/mad.gif'>",$str);
$str = preg_replace(":dizzy:/","<img src='smilies/dizzy.gif'>",$str);
$str = preg_replace("/:nono:/","<img src='smilies/nono.gif'>",$str);
$str = preg_replace("/:eyepatch:/","<img src='smilies/pirate.gif'>",$str);
$str = preg_replace("/:sick:/","<img src='smilies/sick.gif'>",$str);
$str = preg_replace("/:cry:/","<img src='smilies/cry.gif'>",$str);
$str = preg_replace("/:uhh:/","<img src='smilies/uhh.gif'>",$str);
$str = preg_replace("/:wacko:/","<img src='smilies/wacko.gif'>",$str);

?>


Added that to my index.php.. added some smiles.. :-) :-( :D :-D and such.. and it just stays the same.. i have all of the smilies in the folder..
hmmm

pb&j
08-26-2003, 07:23 AM
dunno if this helps...


<?php
$entry = "this is a test :)";
$pattern = ":\)";
$replacement = "<img src=\"smilie.gif\">";
$entry = ereg_replace($pattern,$replacement,$entry);
echo "$entry";
?>

mr_ego
08-26-2003, 12:25 PM
I reckon it would be good to do everything in a database.


CREATE TABLE `smilies` (
`text` VARCHAR( 3 ) NOT NULL ,
`image` VARCHAR( 15 ) NOT NULL ,
UNIQUE (
`text`
)
);


Then for the php ...


function dosmilies($str) {
$sql = "SELECT * FROM smilies";
$query = mysql_query($sql);
while ($_EMO = mysql_fetch_array($query)) {
$str = str_replace($_EMO['text'], "<img src='{$_EMO['image']}'>", $str);
}
return $str;
}


What do you think of that? :D

Comglomo
08-26-2003, 02:03 PM
Ok.. I was thinking.. is this feature to php just like the function stripslashes?
If it is.. then nevermind.. i think i know how to do it. :D

mr_ego
08-27-2003, 11:06 AM
yup