Page 1 of 1

check for only characters in a string

Posted: Tue Aug 07, 2007 4:16 am
by dream2rule
Hello All,

I would like to know a method to check for characters in a given string.

i.e, if the string contains any number(or numbers) an error message should pop up.

Only when the string contains all characters, the data must be inserted into a table or else an error message should pop up.

is_string() considers even numbers if they are specified in quotes.

I don't want that to occur as the field is of type string, only characters need to be inserted into the table.

Any functions in PHP that would do this??

Regards,
Dream2rule

Posted: Tue Aug 07, 2007 4:22 am
by mikeeeeeeey
check out is_numeric

I'd read the documentation first, but I think this should help you

Posted: Tue Aug 07, 2007 4:23 am
by dream2rule
is_numeric checks for the numbers and returns a numeric string :(

I would want to check for characters in a string.

like if i have

$str = "1234567890";
this string value should not be inserted into the database.

and if $str="hdghdgfgdsf";
This should be allowed.

Posted: Tue Aug 07, 2007 4:27 am
by mikeeeeeeey
Have you read the documentation?

Code: Select all

Return Values

Returns TRUE if var is a number or a numeric string, FALSE otherwise.
so all you'd need is...

Code: Select all

if(is_numeric($your_var)){
  //code to be executed
}
or am I missing something?

Posted: Tue Aug 07, 2007 4:35 am
by dream2rule
I want to check for alphabetical characters not numbers!

Posted: Tue Aug 07, 2007 4:41 am
by mikeeeeeeey

Code: Select all

if(!is_numeric($your_var)){
  //code to be executed
}

Re: check for only characters in a string

Posted: Tue Aug 07, 2007 5:05 am
by stereofrog
dream2rule wrote:Hello All,

I would like to know a method to check for characters in a given string.

i.e, if the string contains any number(or numbers) an error message should pop up.

Only when the string contains all characters, the data must be inserted into a table or else an error message should pop up.

is_string() considers even numbers if they are specified in quotes.

I don't want that to occur as the field is of type string, only characters need to be inserted into the table.

Any functions in PHP that would do this??
You need to get your terminology straight. A digit is one of 01234567890 (in english and most latin alphabets). A number is something like 123 or -456.789, but can also be +1.2e-25 or even 0xCafeBabe. A (non-unicode) character is every symbol possible in current encoding, which usually includes 256 different symbols. A letter (in the english alphabet) is A, B etc till Z. A string is a sequence of characters. A numeric string is a string that looks like a number.

Now, when you know that, can you tell us, what your question is? ;)

Posted: Tue Aug 07, 2007 5:07 am
by superdezign
dream2rule wrote:I want to check for alphabetical characters not numbers!
ctype_alpha()