在PHP中返回字符串中间的字符串

using the function substr() how do I remove First and Last THREE letter in a string and return the ones remaining in the middle? eg:

$a = 'abc34828xyz';
$a = 'abc347283828xyz';
$a = 'abc347w83828xyz';
// return first 3
return $first = substr($a, 0, 3);
// return last 3
return $last = substr($a, -3);
// return the string in middle
// $mid = ? now how do we always get the ones in the middle

This should work for you:

(Just start with an offset of 3 and then take the length where you subtract 2*3 (6))

echo $middle = substr($a, 3, strlen($a)-6);

You can get it using preg_replace:

$a = 'abc347w83828xyz';
$mid = preg_replace('/...(.*).../', '$1', $a);
echo $mid, PHP_EOL;

Output:

347w83828

And if you want you can actually get them all at once by using a preg_match call:

$a = 'abc347w83828xyz';
preg_match('/(...)(.*)(...)/', $a, $matches);

echo "First: ", $matches[1], PHP_EOL;
echo "Mid:   ", $matches[2], PHP_EOL;
echo "Last:  ", $matches[3], PHP_EOL;

Output:

First: abc
Mid:   347w83828
Last:  xyz