Jump to content

Archived

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

Eckstra

Searching an array created by file ()

Recommended Posts

Hey everyone.

I'm trying to search through an array created by file (), which will have been a website, for example:

[code]
$lines = file(http://media.putfile.com/Young-Cats-Play-fighting);
[/code]

so each line of the source code from this page on putfile is stored seperately in the array.
The whole idea is for the PHP script to search the source code for the URL to the video file. I would have thought the easiest way would be to use array_search to search the array for wmv, mpg, avi or any other video file extensions, and then go from there. However when I do this the search comes out with no results, possibly because "wmv" or "avi" would only one part of a whole line?

Could anyone help me out?

Share this post


Link to post
Share on other sites
<?php
// Get a file into an array.  In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.example.com/');

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
  echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}

// Another example, let's get a web page into a string.  See also file_get_contents().
$html = implode('', file('http://www.example.com/'));
?> 

link
http://uk.php.net/file

Share this post


Link to post
Share on other sites
Thanks for the reply redarrow. Getting hold of the HTML code isnt a problem, it's getting the script to find the URL to the video file that I cant figure out how to do.

Share this post


Link to post
Share on other sites
the link provides meny solutions with related php code statements.

Share this post


Link to post
Share on other sites
try using the stristr function

[code]
$file = file("path");

foreach($file as $line) {
  if (stristr($line,'.mpg')) {
more code to get rid of any unwanted characters
  }
}
[/code]

Share this post


Link to post
Share on other sites
http://www.webmasterworld.com/forum88/938.htm

exsplode

Share this post


Link to post
Share on other sites
not sure if you were looking for all instances of movie type files? also, not sure what part you are actually looking for.  the actual movie name? movie name plus extension? entire url? well this code looks for all instances of the movie types defined in $movietypes array and outputs each one in the following format:

src="http://url/of/movie/movie.mpg"

so you are going to have to tweak the pregmatch a bit if you want something specific within that string. hope this points you in the right direction.

[code]
<?php
  //movie types. add to the list for more types
  $movietypes = array('mpg','wmv','avi');
 
  //file source
  $lines = file('http://media.putfile.com/Young-Cats-Play-fighting');
 
  foreach ($lines as $codeline) { //foreach line of the filesource
      foreach ($movietypes as $type) { //foreach type of file
        //find the needle in the haystack
        preg_match("|src=\"(.*).".$type."\"|U",$codeline, $out);
        //if something was found
        if ($out[0] != '') {
            //make an array for each item found
            $found[] = $out[0];
        } 
      }
  }
  //example of accessing/echoing out the info
  foreach($found as $val) {
      echo $val . "<br>";
  }
?>
[/code]

Share this post


Link to post
Share on other sites
Thanks Crayon Violent, with a few small changes that worked perfectly. The problem now is forcing the file to be downloaded rather than streamed. I can't change the .htaccess to force files to be downloaded, because they're not being hosted locally, I cant seem to get it to work by playing around with the headers, and I'm not willing to just display the link as text, because it might encourage users to hotlink to the video rather than download it, and I'd like to keep things as simple for the user as possible, some might not understand "right click save target as".

Whever I try and change the headers I get the error

Warning: Cannot modify header information - headers already sent by (output started at /home/blah/test5.php:13) in /home/blah/test5.php on line 17

Can anyone help?

Share this post


Link to post
Share on other sites

×

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.