Trimming a variable

In a CMS I have a variable that contains image id(s).

$article[‘Image’]

Value can be a single image id number like 5 or multiple id’s separated with commas like 12,19,23.

Then I have a plugin for the cms that uses this variable, but fails in case of multiple image ids.

This plugin only needs to use one image id, so how could I trim the multiple id value to one id?

For example single value 5 would stay as 5, but 12,19,23 would be trimmed to the first number 12.

Any help would be appreciated, thanks.

Hi there,

Have a look at the following:
[php]$id = “5”;
$split = explode(",",$id);
$imgid = $split[0]; //$imgid = “5”

$id = “12,19,23”;
$split = explode(",",$id);
$imgid = $split[0]; //$imgid = “12”
[/php]

Basically, pass the variable with your id(s) in it to the explode function, then use the first element of the array. Let me know if you need further help/explanation.

Hi Smokey!

This works perfectly. Thanks for the prompt reply and solution. So simple. It is time for me to take PHP 101.

Sponsor our Newsletter | Privacy Policy | Terms of Service