真实的情况不起作用

1 php if-statement substring

我在PHP函数中有一个像'nameabc'的字符串.我检查字符串的最后三个字符是否为'abc'它应该删除它并返回剩余的字符串.这是我的条件:

$name="nameabc";
$last_char=substr($name,-4);   //it returns the 'abc'
if($last_char == 'abc')   //this condition does not return true
$real_name=substr($name,0,-4);
Run Code Online (Sandbox Code Playgroud)

我不知道是什么问题.

Joh*_*nde 7

substr()eabc你的例子中返回.您需要使用-3的偏移量:

$name="nameabc";
$last_char=substr($name,-3);  
if($last_char == 'abc')  
Run Code Online (Sandbox Code Playgroud)

演示