unset last item of array

Use explode instead of preg_split. It is faster. Then you can use array_pop and array_shift to remove an item from the end and beginning of the array. Then, use implode to put the remaining items back together again.

A better solution would be to use str_pos to find the first and last _ and use substr to copy the part inbetween. This will cause only one sting copy, instead of having to transform a string to array, modify that, and put the array together into a string. (Or don't you need to put them together? The 'I need 'os_disk' at the end confuses me).


$item[$fieldneedle] = " node_os_disk_danger ";
$status = preg_split('/_/',$item[$fieldneedle]);
$status = array_slice($status, 1, -1);

Well, if you want the result to be a string, why bother converting to a string?

$regex = '#^[^_]*_(.*?)_[^_]*$#';
$string = preg_replace($regex, '\\1', $string);

It replaces everything up to and including the first underscore character, and everything after and including the last underscore character. Nice, easy and efficient...


array_shift($end); //removes first
array_pop($end); //removes last

Tags:

Php

Arrays

Regex