Jump to content

Help with PHP Array


kuimera

Recommended Posts

Hi guys,

 

I have a php plugin that I use for smilies, it uses array to pinpoint what code is used for each smile image for example:

$people = array (
  ':)' => 'smile.png',
  ':laughing:' => 'laughing.png',
  ':blush:' => 'blush.png',
  ':d' => 'smiley.png',

now, I'm having trouble adding two types of code for each image without duplicating the smile image in the plugin, I have tried to use:

':d',':D' => smiley.png,

but I had no success because it seem to work but it mess up my plugin... 

 

Any ideas? Can someone help me on this please?

 

 

Link to comment
https://forums.phpfreaks.com/topic/297170-help-with-php-array/
Share on other sites

You're going to have to have 2 array entries for each of your shortcuts, but if you don't want to double up the name for the smiley, you could use a constant

 

define("SMILIES__SMILE", "smiley.png");
$people = array(
  ":d" => SMILIES__SMILE,
  ":D" => SMILIES__SMILE
);

 

That way, you still only have 1 place where you define the actual name for the smiley image.

 

Denno

Link to comment
https://forums.phpfreaks.com/topic/297170-help-with-php-array/#findComment-1515556
Share on other sites

Alternatively

$images = array (
    1 => 'smile.png',
    2 => 'laughing.png',
    3 => 'blush.png',
    4 => 'smiley.png'
    );

$people = array (
  ':)' => 1,
  ':laughing:' => 2,
  ':blush:' => 3,
  ':d' => 4,
  ':D' => 4
  );

But the question is "why are you imposing an artificial constraint that makes life difficult for you when this:

$people = array (
  ':d' => 'smiley.png',
  ':D' => 'smiley.png'
  );

is a perfectly legitimate array?"

Link to comment
https://forums.phpfreaks.com/topic/297170-help-with-php-array/#findComment-1515557
Share on other sites

Alternatively

$images = array (
    1 => 'smile.png',
    2 => 'laughing.png',
    3 => 'blush.png',
    4 => 'smiley.png'
    );

$people = array (
  ':)' => 1,
  ':laughing:' => 2,
  ':blush:' => 3,
  ':d' => 4,
  ':D' => 4
  );

But the question is "why are you imposing an artificial constraint that makes life difficult for you when this:

$people = array (
  ':d' => 'smiley.png',
  ':D' => 'smiley.png'
  );

is a perfectly legitimate array?"

 

Forget to mention that there is a panel that popup to pick the smilies, so having two lines would duplicate the image on that panel..

 

CLqmE7t.png

 

Either way, thank you for the reply Denno and Barand, I'm going to try your solution.

Link to comment
https://forums.phpfreaks.com/topic/297170-help-with-php-array/#findComment-1515560
Share on other sites

Why not just make the replacement of the smiley codes be case-insensitve? 

function imageTag($value) {
    return '<img src="' . $value . '" />';
}
$message = str_ireplace( array_keys($people)
                       , array_map('imageTag', array_values($people))
                       , $message); 

Now you dont need to define : d or : D  as smiley.gif

 

Alternatively use array_unique when displaying your simileys

 

EDIT: Damn stupid form keeps rendering the simely codes, even when wrapped in in nobbc tag

Link to comment
https://forums.phpfreaks.com/topic/297170-help-with-php-array/#findComment-1515566
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.