php - easier way to validate $_GET id's -
in url i'm passing id's this
localhost/?id=1,2,3,4,5,6,7,8,9
i wondering if there better way / simpler way validate each id without using loop?
if (isset($_get['id']) && !empty($_get['id'])) { $str = explode(',', $_get['id']); for($ids = 0; $ids < sizeof($str); $ids++) { if (!ctype_digit($str[$ids])) { echo 'error'; break; } } }
you test string simple regular expression, eg
if (preg_match('/^(\d+,)*\d+$/', $_get['id']) == 0) { throw new exeption('error'); }
Comments
Post a Comment