php - Check input contains either pure numeric characters or pure alpha character using single regular expression -


i have function return true if input pure numeric or alphabate else return false. function working fine.

function checktype($a) {     if (preg_match('/^\d+$/', $a)) { //check numeric (can use other numeric regex /^[0-9]+$/ etc)         $return = true;     } else if (preg_match('/^[a-za-z]+$/', $a)) { //check alphabates         $return = true;     } else { //others         $return = false;     }     return $return; }  var_dump(checktype('abcdfekjh')); //bool(true) var_dump(checktype('1324654')); //bool(true) var_dump(checktype('1324654hkjhkjh'));//bool(false) 

no tried optimized function removing conditions modified code to:

function checktype($a) {     $return  = (preg_match('/^\d+$/', $a) || preg_match('/^[a-za-z]+$/', $a)) ? true:false;     return $return; }  var_dump(checktype('abcdfekjh')); //bool(true) var_dump(checktype('1324654')); //bool(true) var_dump(checktype('1324654hkjhkjh'));//bool(false) 

now in third step tried merge both regex in single regex can avoid 2 preg_match function , got stuck here:

function checktype($a) {     return (preg_match('regex check either numeric or alphabates', $a)) ? true:false; } 

i tried lot of combinations since 2 days using or(!) operator using not operator(?!) no success @ all.

below reference website pick expression , made combinations:

http://regexlib.com/userpatterns.aspx?authorid=26c277f9-61b2-4bf5-bb70-106880138842

http://www.rexegg.com/regex-conditionals.html

or condition in regex

regex not operator (come know not operator)

https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=utf-8#q=regular+expression+not+condition (come know not operator)

so here main question is, there single regex pattern check string contains pure numeric value or pure alphabates?

note: alternative solution can check string alphanumeric , return true or false accordingly. php inbuilt function is_numeric , is_string can used, more curious know single regex pattern check weather string conains pure numeric digit or pure alphaba digits.

a 1 regex check if string ascii digits or ascii letters is

'/^(?:\d+|[a-za-z]+)$/' 

see regex demo

this regex has 2 things regexps not have:

  • a grouping construct (?:....)
  • an alternation operator |.

explanation:

  • ^ - start of string
  • (?:\d+ - 1 or more digits
  • | - or...
  • [a-za-z]+) - 1 or more ascii letters
  • $ - end of string

if need make unicode-aware, use [\p{l}\p{m}] instead of [a-za-z] (and \p{n} instead of \d, not necessary) , use /u modifier:

'/^(?:\p{n}+|[\p{l}\p{m}]+)$/u' 

and in case want check beginning end, use

'/\a(?:\p{n}+|[\p{l}\p{m}]+)\z/u'   ^^                        ^^ 

or

'/^(?:\p{n}+|[\p{l}\p{m}]+)$/du' 

the $ without /d modifier not match string @ "very end", matches if there newline after last character.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -