Ninjakreborn Posted February 7, 2007 Share Posted February 7, 2007 Is strpos() as affective as straight regular expressions for finding a basic string inside another string. For example, validating filetypes (Other filetypes). Say I wanted to allow only .mov, Is strpos checking for mov inside a filetype going to be safe, or testing for .mov. Is this going to be as affective as setting it up to test with regular expressions? Link to comment https://forums.phpfreaks.com/topic/37467-solved-strpos-effectiveness/ Share on other sites More sharing options...
trq Posted February 7, 2007 Share Posted February 7, 2007 If you know the exact string your looking for strpos() is much more efficient. Link to comment https://forums.phpfreaks.com/topic/37467-solved-strpos-effectiveness/#findComment-179149 Share on other sites More sharing options...
Ninjakreborn Posted February 7, 2007 Author Share Posted February 7, 2007 Good that is exactly what I wanted to hear. Thanks. Link to comment https://forums.phpfreaks.com/topic/37467-solved-strpos-effectiveness/#findComment-179153 Share on other sites More sharing options...
.josh Posted February 7, 2007 Share Posted February 7, 2007 One thing I like about the manual is that it is usually really good about telling you things like this. Tip Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster. Link to comment https://forums.phpfreaks.com/topic/37467-solved-strpos-effectiveness/#findComment-179154 Share on other sites More sharing options...
effigy Posted February 7, 2007 Share Posted February 7, 2007 If you're checking for a string at the end of another string, regular expressions will be slightly easier on the eyes because you can use the \z anchor. If you use strpos, you'll need to calculate the results against both lengths to determine that it is, in fact, at the end. Link to comment https://forums.phpfreaks.com/topic/37467-solved-strpos-effectiveness/#findComment-179155 Share on other sites More sharing options...
Ninjakreborn Posted February 7, 2007 Author Share Posted February 7, 2007 I know, but I am running it through an array. I wanted to know if it's 100% safe. I want to run it through something like. $extensions = array(".wav", ".mov", ".swf"); // plus whatever else will be here foreach ($extensions as $k => $v) { if (strpos($extensions, $file)) { req } ah whatever I will get it together when I get started, but that's the basic idea. I am going to use strpos to validate the filetype's. Link to comment https://forums.phpfreaks.com/topic/37467-solved-strpos-effectiveness/#findComment-179160 Share on other sites More sharing options...
Ninjakreborn Posted February 7, 2007 Author Share Posted February 7, 2007 If you're checking for a string at the end of another string, regular expressions will be slightly easier on the eyes because you can use the \z anchor. If you use strpos, you'll need to calculate the results against both length's to determine that it is, in fact, at the end. I am just frustrated because I had an entire file handling system created I recopied, however accidentally deleted the source file's along time ago, and haven't gotten around to building another one. This project is going to require me to rebuild another one, which I am looking for quicker way's to get this up and running. I may just use regular expressions. Thanks for all the advice. Link to comment https://forums.phpfreaks.com/topic/37467-solved-strpos-effectiveness/#findComment-179163 Share on other sites More sharing options...
Ninjakreborn Posted February 7, 2007 Author Share Posted February 7, 2007 Incase if it help's anyone I created this <?php function getExtension($file) { $pos = strrpos($file, '.'); if(!$pos) { return 'Unknown Filetype'; } $str = substr($file, $pos, strlen($file)); return $str; } ?> Link to comment https://forums.phpfreaks.com/topic/37467-solved-strpos-effectiveness/#findComment-179181 Share on other sites More sharing options...
trq Posted February 7, 2007 Share Posted February 7, 2007 If your wanting to validate valid filetypes using an array, use in_array(). eg; <?php $file = 'foo.wav'; $validtypes = array('wav','mov','swf'); if (in_array(substr($file,-3),$validtypes)) { // is valid. } ?> Link to comment https://forums.phpfreaks.com/topic/37467-solved-strpos-effectiveness/#findComment-179185 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.