kaotik78 Posted December 20, 2006 Share Posted December 20, 2006 Hi all,Im trying to do the following with php and finding it difficult to get it to do what I need. Im trying to have php check a variable which has another variable assigned to it, for a video file extenstion name then proceed with if/else. When you run the page $result[0][1]; would have M23432.MPG as the actual value. However the result on the page continually says false, there is no MPG in the string. the string 'mpg' was not found in '1'$pos==; type is booleanIt only seems to work if I manually assign $mystring = 'whatever.mpg'; rather than have it pulled dynamically. Any ideas?? <?php $mystring = $result[0][1]; $mystring = settype($mystring, "int"); $findme = 'mpg'; $pos = strpos($mystring, $findme); if ($pos === false) {print "the string '$findme' was not found in '$mystring'";echo "\$pos==$pos; type is " . gettype ($pos) . "<br />\n"; } else {print "<a href=\"pictures\\{$mystring}\" target=\"_blank\">Click here to view video</a>";} Quote Link to comment Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 Why do you use settype() ?[code]<?phpif(stripos($result[0][1], "mpg") === FALSE) echo "Not found";else echo "Found";?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
kaotik78 Posted December 20, 2006 Author Share Posted December 20, 2006 Im still pretty new to php so im just trying what I think (in my mind) would work :) apparently it isn't.. Quote Link to comment Share on other sites More sharing options...
kaotik78 Posted December 20, 2006 Author Share Posted December 20, 2006 Ok now I just spent a day and a half trying to figure this out, talk about being green to php... your code works! I have to research how so I can better understand what it did! Thank you for a push in the right direction! Quote Link to comment Share on other sites More sharing options...
kaotik78 Posted December 20, 2006 Author Share Posted December 20, 2006 Ok, bear with me. I still don't understand the syntax. I guess I can't search for multiple extensions, your limited to 3 php says? I put the extensions in a variable but it does not work. Is there a function I need to wrap the variable in for stripos to search properly?$vidext = "mov,mpg,avi,wmv";if(stripos($result[0][1], "mpg") === FALSE) echo ""; Quote Link to comment Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 You can use an array and a little loop:[code]<?php$vidext = array("mov","mpg","avi","wmv");foreach ($vidext as $ext){ if(stripos($result[0][1], $ext) !== FALSE) echo "Has ".$ext." in the filename";}?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
kaotik78 Posted December 20, 2006 Author Share Posted December 20, 2006 ok, last one cause im kinda getting it now. What could I use to parse the filename to actually check the file extension. Say someone uploads "mov234.mpg" it would show twice cause mov and mpg are both listed. Can I parse the filename to search for "." then search after the "."? Quote Link to comment Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 To get the extension, I always use this:[code]<?php//$filename contains the filename$extension = strtolower(substr(strrchr($filename,"."),1));?>[/code]strrchr() get's everything from the last dot in the string to the end, substr() removes the dot from the begining and strtolower() lowercases it :)Orio. Quote Link to comment Share on other sites More sharing options...
kaotik78 Posted December 20, 2006 Author Share Posted December 20, 2006 ok so here is what I have thus far with your help. When I get to the page with a .mpg loaded it shows the link as "Not foundNot foundClick here to view videoClick here to view video" even on a page that has a jpg on it. What have I screwed up or do I have the array/loop wrong?<?php//$filename contains the filename$extension = strtolower(substr(strrchr($result[0][1],"."),1));$vidext = array("mov","mpg","avi","wmv");foreach ($vidext as $ext) {if(stripos($result[0][1], $ext) !== FALSE) echo "Not found";else echo "<a href=\"pictures\\{$result[0][1]}\" target=\"_blank\">Click here to view video</a>";}?> Quote Link to comment Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 That needs to be a bit diffrent:[code]<?php$extension = strtolower(substr(strrchr($result[0][1],"."),1));$vidext = array("mov","mpg","avi","wmv");if(in_array($ext, $vidext) echo "<a href=\"pictures\\".$result[0][1]}."\" target=\"_blank\">Click here to view video</a>";else echo "Not a video.";?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
kaotik78 Posted December 20, 2006 Author Share Posted December 20, 2006 Weird, it threw up a error "Parse error: parse error, unexpected T_ECHO in C:\Documents and Settings\Owner.laptop\Desktop\gallery\skins\rounded\templates\viewimage_begin.php on line 44"line 44 isecho "<a href=\"pictures\\".$result[0][1]}."\" target=\"_blank\">Click here to view video</a>";I tried echo "video" to see if it was some syntax not being parsed correctly but it still puts up the error. $ext is being used still but not defined, is that the problem? Quote Link to comment Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 I forgot one ")":[code]<?php$extension = strtolower(substr(strrchr($result[0][1],"."),1));$vidext = array("mov","mpg","avi","wmv");if(in_array($ext, $vidext)) echo "<a href=\"pictures\\".$result[0][1]}."\" target=\"_blank\">Click here to view video</a>";else echo "Not a video.";?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
kaotik78 Posted December 20, 2006 Author Share Posted December 20, 2006 I saw that only by researching the syntax, it is still giving me problems.I have the following. The result isParse error: parse error, unexpected '}', expecting ',' or ';' in I tried putting { } 's after the ending ) on the if statement but that wasn't what it was looking for. The $ext confused me as $ext is not defined, but $extension is, does php just look for the matching few letters of the variable or am I missing something?<?php$extension = strtolower(substr(strrchr($result[0][1],"."),1));$vidext = array("mov","mpg","avi","wmv");if(in_array($extension, $vidext)) echo "<a href=\"pictures\\".$result[0][1]}."\" target=\"_blank\">Click here to view video</a>";else echo "Not a video.";?> Quote Link to comment Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 My bad again, sorry.[code]<?php$extension = strtolower(substr(strrchr($result[0][1],"."),1));$vidext = array("mov","mpg","avi","wmv");if(in_array($ext, $vidext)) echo "<a href=\"pictures\\".$result[0][1]."\" target=\"_blank\">Click here to view video</a>";else echo "Not a video.";?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
kaotik78 Posted December 20, 2006 Author Share Posted December 20, 2006 Orio,Don't appologize, afterall your the one who's helping me!The end result is what's below. The $ext didden't work so I changed it to $extension and viola it's working. It helped me fill in the blanks and figure out why it wasn't working. The syntax will take getting used to as i've only been doing this for a day, but thank you very much for your help!<?php$extension = strtolower(substr(strrchr($result[0][1],"."),1));$vidext = array("mov","mpg","avi","wmv");if(in_array($extension, $vidext)) echo "<a href=\"pictures\\".$result[0][1]."\" target=\"_blank\">Click here to view video</a>";else echo "Not a video.";?> 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.