Solved

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

Posted on 2004-09-09
4
2,593 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
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 dynamically set the form action using jQuery.
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now