?
Solved

Regual expressions for detecting numeric, alpha numeric and alpha strings

Posted on 2005-04-14
4
Medium Priority
?
893 Views
Last Modified: 2012-08-13
Hi Everyone,

I wrote the following patterns to use in eregi():

- For detecting if the string is totally numeric
"^[0-9]*$"

- For detecting if the string is totally alpha
"^[a-zA-Z]*$"

- For detecting if the string is totally alpha numeric
"^[a-zA-Z_0-9]*$"

But i have a problem in alpha and alpha numeric detection patterns. These two patterns must also accept spaces. How can i add the space to the patterns?

Thank you!
0
Comment
Question by:blacklord
  • 3
4 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 13782500
\s is the special character for spaces. You could also use \W in preg_replace instead of [a-zA-Z0-9_]

example:

<?php
$string = "alfabeta09394! ?alfa";
$string = preg_replace("/[\W\s]/s","",$string);
echo $string;

?>

0
 
LVL 32

Expert Comment

by:Batalf
ID: 13782532
Sorry - you should use \w instead of \W (lowercase intead of uppercase). I misread your question and thought you wanted to remove everything EXCEPT alphanumeric characters.

The corrected versions of the code should be:

<?php
$string = "alfabeta09394! ?alfa";
$string = preg_replace("/[\w\s]/s","",$string);
echo $string;

?>

\w represents all alpha numeric characters, while \W represents all non-alphanumeric characters.

Batalf
0
 
LVL 32

Accepted Solution

by:
Batalf earned 700 total points
ID: 13782606
Sorry for all the posts. Have been a very long day. Didn't intend to send all these posts or tag the question:-)

You're trying to match the string against a pattern.

Then the pattern should be

"/^[\w\s]+$/s";

i.e.

^ = start of strong
[\w\s]+ = followed by any number of alphanumeric characters or spaces
$ = end of string.

Example:

<?

// Example one:
$string = "alfabeta09394 ! ?alfa";
$pattern = "/^[\w\s]+$/s";
if(preg_match($pattern,$string)){
    echo "Alpha characters only<br>";    
}else{
    echo "String contains non alphanumeric characters<br>";
}

// Example two:
$string = "alfabeta09394alfa";
if(preg_match($pattern,$string)){
    echo "Alpha characters and/or spaces only";    
}else{
    echo "String contains non alphanumeric characters";
}

 
 ?>
0
 
LVL 25

Assisted Solution

by:Marcus Bointon
Marcus Bointon earned 300 total points
ID: 13783157
Using a regex to spot purely numeric values is a bit wasteful - you can just use is_numeric(): http://www.php.net/manual/en/function.is-numeric.php Otherwise Batalf has it taped.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
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…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 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