You can use regex:
$str = preg_replace('~^(.)(.*)\1$~','$2',$str);
Regex explanation:
~: delimiters^: Launch anchor(.): match and remember char (here is its first char)(.*): match something and remember\1: remember the first match$: End anchor$2: remember the second match
Alternatively, you can:
// if string has >1 char and 1st and last char as same.
if(strlen($str) > 1 && $str[0] == $str[strlen($str)-1]) {
$str = substr($str,1,strlen($str)-2); // extract the substring
}
source
share