Here is a PHP function that takes a string as input and returns an array containing the last two elements:

function getLastTwoElements($input) {
    $elements = explode(" ", $input);
    $lastTwo = array_slice($elements, -2);
    return $lastTwo;
}

$input = "abc d aa an 3";
$result = getLastTwoElements($input);
print_r($result);

Output:

Array
(
    [0] => an
    [1] => 3
)

The function getLastTwoElements first splits the input string into an array using the delimiter " ". Then, it uses array_slice to extract the last two elements from the array. Finally, it returns the resulting array

make php function for me input abc d aa an 3 return abc d aaan3 ONLY put last 2 elements

原文地址: https://www.cveoy.top/t/topic/iqMm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录