php - Need help with regular expression for URLs -
was parsing urls of type http://sitename.com/catid/300/, http://sitename.com/catid/341/ etc etc
wherein parameter after catid (300,341) integers. when use following condition in .htaccess, works fine
rewriterule ^(abc|def|hij|klm)/([0-9]+)/$ /index3.php?catid=$1&postid=$2 [l]
but when php regex match function,like preg_match, returns 1 alphanumeric numbers too
eg: echo preg_match('([0-9]+)','a123'); or echo preg_match('([0-9]+)',a123);
they both return 1 ()true. dont know why matches alphanumerics. want match numbers.
what doing wrong?
without anchors, match numbers regardless of comes before or after them. try anchors:
/^([0-9]+)$/
if don't need capture, try this:
/^[0-9]+$/
results:
php > echo preg_match('([0-9]+)','a123'); 1 php > echo preg_match('/^[0-9]+$/','a123'); 0 php > echo preg_match('/^[0-9]+$/',0.65); 0 php > echo preg_match('/^[0-9]+$/',666); 1
regular expressions not answer. if checking number, try is_numeric. if want integer, try is_int.
Comments
Post a Comment