Solved

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

Posted on 2004-09-09
4
2,606 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
  • 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 333 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 49
How to send multiple emails at the same time in PHP 12 58
How to count in a table in php 22 35
Is there a limit to how many inner joins you should use? 4 30
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 …
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…
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…
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…

770 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