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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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; } ?> Quote Link to comment 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. } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.