Solved

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

Posted on 2012-04-12
3
289 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 200 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 13

Accepted Solution

by:
Phil Phillips earned 200 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Display images from mysql blob type (Not working) 9 41
Decrypt string by php 7 47
Read a remote csv file from a https URL 8 47
Log in through ID 5 17
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

840 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