array_merge & strtr() doesn't work

546 views Asked by At

Hi I have a problem with strtr().

I am creating a website where users can add their emoticons, and call them inside their posts by typing in the special code for an emoticon.

This is what I have done and it works to some extent:

//FETCH FROM THE DB

while ( $row = mysql_fetch_array($fetch)) {
$table[] = array(
$row['shortcuttag1'] => "<img class='x' src='" . $row['tag1smilo'] . "' />",
$row['shortcuttag2'] => "<img class='x' src='" . $row['tag2smilo'] . "' />",
$row['shortcuttag3'] => "<img class='x' src='" . $row['tag3smilo'] . "' />",
$row['shortcuttag4'] => "<img class='x' src='" . $row['tag4smilo'] . "' />",
$row['shortcuttag5'] => "<img class='x' src='" . $row['tag5smilo'] . "' />"
);

This creates a multidimensional array with emoticons uploaded by one user.

When I use strtr($txt,$table[0]), it works with the array[0] and the others, but I want to change the special code to emoticons that are located in and around all the subarrays.

Therefore, what I have done is to merge the array like so:

$oneDim = call_user_func_array('array_merge',$table);

I got the one dimensional array with all SpecialCode => Image fields.

But the strtr($txt,$oneDim) stopped working with that; it is not showing anything.

I am worried because I have tried a few different ways to merge the array other than call_user_func_array() and it gives the same result.

Maybe there is someone that could help me with that. I will be very grateful.

Thanks

1

There are 1 answers

0
THE AMAZING On

You're doing it wrong.

Lets say you have a chatbox

+--------------------------------------+
|Tom: Welcome!                         |
|Terry: Hello! :)                      |
+--------------------------------------+-------+
|                                      | SEND  |
---------------------------------------+-------+

You need to parse he string before the output is placed in the textbox, like so:

<?php
   if(strpos($str, ":)")){
         $str = str_replace(':)',"<img src='smiley.png'/>",$str);
   }
?>

when you output the $str var using AJAX you read he data back as HTML.

there is no need for all that unnecessary processing you started to do.

If you need more help with this, i will post a tutorial on my website @ http://www.code-genius.com/ later today after work.