开发者

What is the simplest of checking first and last character of a string in php

Wh开发者_Python百科at is the simplest way of checking whether the first letter of a string $str is 'a' and the last letter is 'a' too?


if($str[0] == 'a' && $str[strlen($str) - 1] == 'a') {
    //do whatever you wanted
}


if(substr($str, 0,1) == "a" && substr( $str,-1) == "a")
{
    // code
}


Or use substr to get the last character:

if($str[0] == 'a' && substr($str,-1) == 'a') {
    //do whatever you wanted
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜