Solved

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

Posted on 2012-04-12
3
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 14

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
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.

734 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