views:

498

answers:

5

Hello,

Having problems with this.

Let's say I have a parameter composed of a single character and I only want to accept alphabetic characters. How will I determine that the parameter passed is a member of the latin alphabet (az)?

By the way Im using PHP Kohana 3.

Thanks.

+4  A: 

Use the following guard clause at the top of your method:

if (!preg_match("/^[a-z]$/", $param)) {
    // throw an Exception...
}

If you want to allow upper case letters too, change the regular expression accordingly:

if (!preg_match("/^[a-zA-Z]$/", $param)) {
    // throw an Exception...
}

Another way to support case insensitivity is to use the /i case insensitivity modifier:

if (!preg_match("/^[a-z]$/i", $param)) {
    // throw an Exception...
}
Asaph
preg_match("/^[a-z]$/i", $param) or use case insensitive
Xorlev
Your comments are backwards. If preg_match succeeds in matching [a-z], then you should not throw an exception. if(!preg_match(...)) { exception! }
Nick Presta
@Nick Presta: Good catch. I added a `!` to the conditions. Thanks :)
Asaph
Regex are fine, but many times you use one, even if trivial, you reinvent the wheel and introduce contact surface for bugs. I'd prefer http://php.net/manual/en/book.ctype.php .
phresnel
A: 

You can try:

preg_match('/^[a-zA-Z]$/',$input_char);

The return value of the above function is true if the $input_char contains a single alphabet, else it is false. You can suitably make use of return value.

codaddict
+3  A: 
preg_match('/^[a-zA-Z]$/', $var_vhar);

Method will return int value: for no match returns 0 and for matches returns 1.

Ruchir Shah
Regex are fine, but many times you use one, even if trivial, you reinvent the wheel and introduce contact surface for bugs. I'd prefer php.net/manual/en/book.ctype.php
phresnel
+7  A: 

http://php.net/manual/en/function.ctype-alpha.php

<?php
    $ch = 'a';
    if (ctype_alpha($ch)) {
        // Accept
    } else {
        // Reject
    }

This also takes locale into account if you set it correctly.

EDIT: To be complete, other posters here seem to think that you need to ensure the parameter is a single character, or else the parameter is invalid. To check the length of a string, you can use strlen(). If strlen() returns any non-1 number, then you can reject the parameter, too.

As it stands, your question at the time of answering, conveys that you have a single character parameter somewhere and you want to check that it is alphabetical. I have provided a general purpose solution that does this, and is locale friendly too.

Nick Presta
But what if `$ch` is more than one character long?
Asaph
From the manual: _Checks if all of the characters in the provided string, text , are alphabetic_ ctype_alpha() accepts a string.
Nick Presta
@Nick Presta: I know what `ctype_alpha()` does. The OP wants to reject all input that is more than one character long and your solution doesn't do that.
Asaph
The OP says that he has a single character and wants to check that it is alphabetical. He does not mention that he needs to restrict this to a single character. His parameter is already a single character, as far as I can tell.
Nick Presta
I had a different interpretation. I suppose the requirements need to be clarified. Let's talk about it with the OP in tomorrow morning's scrum. :)
Asaph
A: 

I'd use ctype, as Nick suggested,since it is not only faster than regex, it is even faster than most of the string functions built into PHP. But you also need to make sure it is a single character:

if (ctype_alpha($ch) && strlen($ch) == 1) {
    // Accept
} else {
    // Reject
}
Anthony
No you don't. ctype_alpha checks the whole string. See the manual.
Nick Presta
I know it does. But he only wants the parameter to be a single character. So if the string is `AB` it should be rejected, regardless of if they both are checked with `ctype_alpha`. See the question.
Anthony
Perhaps _you_ should see the question. The OP states he *already* has a single character parameter. He wants to check if that parameter is alphabetical. Checking the length of the parameter is the responsibility of the caller, or at least shouldn't be handled here as the OP never asked for this, and has nothing to do with the question, "Determining if a character is alphabetic".
Nick Presta