Jump to content

Recommended Posts

Hello im kind of new to regex i know the very very basic basics like [0-9] and [a-z] but i have a few if strings that i need a regex for so i can See if the whole value or string is only numbers or only letters

 

Numbers

1235415=>true

1244p123=>false

.12235135=>false

and so on

 

Letters

aldkfjalsdf=>true

adflkjadfl1=>false

.aldkfjasldf=>false

and so on

 

if (preg_match($match,$string))
		{
		$x="2";
		}

 

any help with this problem(lack of knowledge) would be greatly appreciated

Link to comment
https://forums.phpfreaks.com/topic/80629-new-to-regex/
Share on other sites

I was looking for a regex that tells you that the whole string is or is not only letters or is or isnt only letters

 

1235415=>true (all numbers)

1244p123=>false (not all numbers because of "p")

.12235135=>false (not all numbers because of ".")

and so on

 

Letters

aldkfjalsdf=>true(all letters)

adflkjadfl1=>false(not all letters because of "1")

.aldkfjasldf=>false(not all letters because of ".")

 

would i use preg_match_all will it check to see if the whole string only has only letters or only numbers??

Link to comment
https://forums.phpfreaks.com/topic/80629-new-to-regex/#findComment-410999
Share on other sites

^[0-9]+$ and ^[a-zA-Z]+$ can do that, but why not just use functions like ctype_alpha() to determine if it only consists of alphabetic characters (a-z uppercase and lowercase) and ctype_digit() to determine if it only consists of digits (0-9) (is_numeric() won't work as it will accept floats as well)?
Link to comment
https://forums.phpfreaks.com/topic/80629-new-to-regex/#findComment-411015
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.