Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP : How to check if the string only contains 1 alphabet?

Posted on 2012-04-12
3
Medium Priority
?
321 Views
Last Modified: 2012-06-27
Hie all,

How to do I check if the string only contains 1 alphabet? For example,

$string = 'AAAAAB'  - false
$string = 'AAAAAA'  - true

I have tried using strpos, preg_match but couldnt suit my needs. I just need to make sure that there is all A in the string.

Thanks.
0
Comment
Question by:bosscat
3 Comments
 
LVL 14

Assisted Solution

by:Scott Madeira
Scott Madeira earned 800 total points
ID: 37839488
This worked for me for some simple examples....

<?php

$string = 'AAABBAA';

if (strlen($string) +1 == count(explode(substr($string, 1,1), $string))){
	// They are the same
	echo 'All same characters';
	
} else {
	// They are different
	echo 'Some different characters';
}
?>

Open in new window

0
 
LVL 15

Accepted Solution

by:
Phil Phillips earned 800 total points
ID: 37840333
This is a slightly less complicated variant of the answer above.

<?php

$string = 'AAABBAA';

if (strlen($string) == substr_count($string, $string{0})) {
   // They are the same
   echo 'All same characters';
}
else {
   // They are different
   echo 'Some different characters';
}

?>

Open in new window


Note that both examples are case-sensitive.  So, if you want 'aaaAA' to return 'true', then you could convert the string to lower case (strtolower()) when running the check.
0
 
LVL 1

Author Closing Comment

by:bosscat
ID: 37840653
Thank you guys... Both ways work excellent.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question