替换php中的第一个和最后一个字符[复制]

This question already has an answer here:

A question about php.

If I have variable named $string that contains the word "Testing" I would like the php code to delete the first and last character. (So the output would be "estin"). I've tried multiple functions for example str_replace and substr but so far I've only managed to delete only the first or only the last character.

I don't know how to delete both the first and last character.

</div>
<?php
$str = 'Testing';
$result = substr($str, 1, -1);
echo $result; // estin

the result of the code: http://codepad.org/RLbw4azA

read more about: substr


function: substr can receive 3 parameters:

    string substr (string $string , int $start [, int $length ] )

If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes the position of this truncation or beyond, false will be returned.

$str = "Testing";
echo $str = substr($str,1,-1);

try substr($string, 1, -1). It will help

Have you tried this:

$string = substr($string, 1, -1);

$str = 'ABCDEFGH';

echo $result = substr($str, 1, -1);

//output will be show the BCDEFG

use preg_replace

$string = preg_replace('/^.|.$/','',$string);