开发者

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

开发者 https://www.devze.com 2023-02-11 09:26 出处:网络
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\'

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

精彩评论

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

关注公众号