check for only characters in a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

check for only characters in a string

Post 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
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Post by mikeeeeeeey »

check out is_numeric

I'd read the documentation first, but I think this should help you
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post 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.
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Post 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?
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

I want to check for alphabetical characters not numbers!
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Post by mikeeeeeeey »

Code: Select all

if(!is_numeric($your_var)){
  //code to be executed
}
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Re: check for only characters in a string

Post 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? ;)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

dream2rule wrote:I want to check for alphabetical characters not numbers!
ctype_alpha()
Post Reply