Detecting smileys (how can i do this)

Hi guys, ive been developing my own forum software.

I am up to the stage of viewing smileys on a post once a string has been called, can anyone help me think of a solution if a string is called example :slight_smile: it will convert :slight_smile: to output html image code?

thanks…

[FPHP]str_replace[/FPHP]
[FPHP]preg_replace[/FPHP]

Sorry trying to get it to work but im just confusing myself, this is what i have:

My smileys are stored in a database with tables SmileyImage and SmileyCode
Smiley code is the code to activate the smiley, example :slight_smile: and smileyimage is the name of the smiley, example happy.gif.

I also have a code which displays text from a forum message which if the smileycode is called it will change it into the html code example, <img src=‘images/smileys/<?= $qry13[SmileyImage]’> This comes from the string $mtext. When the code has been processed with the smileys i want the new string to be named $finalmessage.

My code below exports a loop of the smileys from the database:


$result = mysql_db_query($dbname, "SELECT * FROM forum_smileys ");
if (mysql_num_rows($result)) {
   while ($qry13 = mysql_fetch_array($result)) {
$finalmessage = substr_replace($qry13[SmileyCode], "<img src='../images/smileys/$qry13[SmileyImage]'>", 0);
}
}

Any ideas people?

Plenty of ideas, and at least 3 errors.

My smileys are stored in a database with tables SmileyImage and SmileyCode

$result = mysql_db_query($dbname, "SELECT * FROM forum_smileys ");

So the first statement should have been “My smilies are stored in a database table called forum_smileys with fields SmileyImage and SmileyCode”. But that’s just grammatical.


   while ($qry13 = mysql_fetch_array($result)) {

You meant to use mysql_fetch_assoc here, not fetch_array.


$finalmessage = substr_replace($qry13[SmileyCode], "<img src='../images/smileys/$qry13[SmileyImage]'>", 0);

Let’s just clean that up a little bit…


$finalmessage = substr_replace($qry13['SmileyCode'], "<img src='../images/smileys/".$qry13['SmileyImage']."'>", 0);

thank you let me try and i will let you know,

Can you please explain this as i do not understand: You meant to use mysql_fetch_assoc here, not fetch_array.

mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter.

mysql_fetch_assoc() is also doesn’t really need to exist because it just returns the key names insted of Numeric keys and key names which happens in mysql_fetch_array.

‘Associative arrays’ returned by mysql_fetch_assoc() are arrays with key names so, they’re more human readable.

Some times mysql_fetch_assoc() is faster than mysql_fetch_array() .

mysql_fetch_assoc() may also need less memory as it generates an array of key names instead of keynames and key indexes in mysql_fetch_array().

Sorry just abit confused now, we need to detect the smileys from $qry[‘MessageText’] how can we incorperate so $finalmessage replaces the smiley codes with the image?

fetch_assoc will be faster because it retrieves/holds 1/2 the data that fetch_array does. (without an argument, array fills BOTH, so will hold both numeric and associative keys)

And… yes, you’ve gotten your commands mixed up. substr != str.


$finalmessage = str_replace($qry13['SmileyCode'], "<img src='../images/smileys/".$qry13['SmileyImage']."'>", $qry['MessageText']);  

ok at the moment this is the code:

$result = mysql_db_query($dbname, “SELECT * FROM forum_smileys “);
if (mysql_num_rows($result)) {
while ($qry13 = mysql_fetch_assoc($result)) {
$finalmessage = str_replace($qry13[‘SmileyCode’], “<img src='…/images/smileys/”.$qry13[‘SmileyImage’].”'>”, $qry[‘MessageText’]);

}
}

Below im outputting $finalmessage but the smileys do not insert the image they are still displaying as the code, example :slight_smile: :slight_smile: