Solved

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

Posted on 2012-04-12
3
301 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

617 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