Jump to content

convert variable result to string to search with strpos


kaotik78

Recommended Posts

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 boolean

It 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>";
}
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 "";

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 "."?

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.
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>";
}
?>
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.
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 is

echo "<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?
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.
I saw that only by researching the syntax, it is still giving me problems.

I have the following. The result is

Parse 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.";
?>
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.
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.";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.