Fastest way to validate a path with a list of extensions


/ Published in: PHP
Save to your folder(s)

Here is the research of running the scripts 10000 times, in milliseconds:

preg_match('/.*\.(jpg|gif|png)\z/',substr('the image/is on/fire.jpg,-4'))
0.29-0.27

in_array(strtolower(substr('the image/is on/fire.jpg',-3)), array('jpg','gif','png'))
0.03-0.025

switch (strtolower(substr('the image/is on/fire.jpg',-3))){
case 'jpg':
case 'gif':
case 'png':
$ec = true;
}//use $ec for return statement
0.019-0.14


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. switch (strtolower(substr('the image/is on/fire.jpg',-3))){
  4. case 'jpg':
  5. case 'gif':
  6. case 'png':
  7. $ec = true;
  8. }//use $ec for return statement
  9. if($ec){
  10. echo 'T';
  11. }
  12.  
  13. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.