Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

php

What is the simplest way of checking whether the first letter of a string $str is 'a' and the last letter is 'a' too?

like image 811
Vadiklk Avatar asked Feb 17 '11 11:02

Vadiklk


People also ask

How do I get the first and last character of a string in PHP?

Using substr() Method: The substr() is a built-in function in PHP that is used to extract a part of string. Example: For example, if the string is “Akshit loves GeeksForGeeks”. The last character of the string is “s”.

How do I find the starting character of a string in PHP?

We can test if a certain string is the exact start of another string: <? php function startsWith($string, $startString) { $len = strlen($startString); return (substr($string, 0, $len) === $startString); } // usage echo startsWith("cat", "c"); // true echo startsWith("dog", "x"); // false ?>

What is the first character of any PHP variable?

Naming and Creating a Variable in PHP The first character of the name must be either a letter or an underscore (_). The remaining characters must comprise only of letters, numbers or underscores.

What is the value of the last character in a string?

The first character in a string is present at index zero and the last character in a string is present at index length of string-1 .


1 Answers

if(substr($str, 0,1) == "a" && substr( $str,-1) == "a")
{
    // code
}
like image 162
TNC Avatar answered Oct 20 '22 22:10

TNC