2

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

Agi Hammerthief
  • 2,114
  • 1
  • 22
  • 38
Dave
  • 29
  • 1
  • 4
    Why not use [str_replace](http://php.net/manual/en/function.strstr.php) in the first place? – dbf Nov 03 '12 at 21:39
  • 3
    @Baba `$table[] = array();` definitely looks like having an index 0 and also a lot more indices depending on how many `mysql_fetch_array` will return. – dbf Nov 03 '12 at 21:44
  • Hey guys! str_replace works fine thanks very much. – Dave Nov 03 '12 at 21:45
  • @dbf ..you are right ... nice one +1 – Baba Nov 03 '12 at 21:46
  • 1
    dbf, can you please add your answer to this question so that the question can be marked as "answered" by Agi Hammerthief? – Jared Clemence Feb 09 '15 at 23:51

1 Answers1

0

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.

THE AMAZING
  • 1,496
  • 2
  • 16
  • 38