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

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -