Jump to content

New to Regex


cry of war

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.