Solved

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

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

695 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