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
Share on other sites

So you're trying to add ? Copy the line for  and change it to a capital D.

 

Thank you for the quick reply req, I want both 


and 


for the same image without duplicating the image or line for that mater..

Edited by kuimera
Link to comment
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
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?"

  • Like 1
Link to comment
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
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

Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.