?
Solved

How can I deturmine if a string contains any letters or numbers?

Posted on 2004-09-09
4
Medium Priority
?
2,617 Views
Last Modified: 2011-10-03
Hello.

How can I deturmine if a string contains any letters or numbers?


Right now I am using this:

       if ($string != null) { echo "contains text"; }

But if the string has only spaces and new lines in it, the script returns "contains text"
I want it to do this only if there are letters or numbers in the string.

Thanks!
0
Comment
Question by:hankknight
[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
  • 2
4 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 12016490
You can try:

if (strlen(trim($string)) > 0) { echo "contains text.";}

trim removes any whitespace at the end and beginning of a string.
0
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12016630
Try this:

if(ereg('[^A-Za-z0-9]', $string)){
    echo "String must contain only letters and numbers.";
}else{
    echo "$string has only letters and numbers.";
}
0
 
LVL 16

Author Comment

by:hankknight
ID: 12017046
Thanks!

       if (strlen(trim($string)) > 0) { echo "contains text.";}

would work exept if my string contains funny characters ( ., ?, *, ©, etc ) it would still pass the test.

The ereg option is closer:

        if(ereg('[^A-Za-z0-9]', $string)){
            echo "String must contain only letters and numbers.";
        }else{
            echo "$string has only letters and numbers.";
        }

But this tells me if the string contains anything other than letters and numbers.  I want to find out if the string contains at least one letter or one number.
0
 
LVL 13

Accepted Solution

by:
cLFlaVA earned 1332 total points
ID: 12017258
if(ereg('[A-Za-z0-9]', $string)){
            echo "Contains at least one letter or number";
        }else{
            echo "Does not contain letters or numbers.";
        }
0

Featured Post

Independent Software Vendors: 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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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.
Suggested Courses

764 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