Jump to content

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?
Link to comment
https://forums.phpfreaks.com/topic/13466-searching-an-array-created-by-file/
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
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]
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?
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.