Jump to content


Photo

Searching an array created by file ()


  • Please log in to reply
7 replies to this topic

#1 Eckstra

Eckstra
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 July 2006 - 04:20 PM

Hey everyone.

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

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

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?

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 July 2006 - 04:33 PM

<?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
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 Eckstra

Eckstra
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 July 2006 - 04:46 PM

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.

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 July 2006 - 04:48 PM

the link provides meny solutions with related php code statements.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 Drumminxx

Drumminxx
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts
  • LocationUSA

Posted 02 July 2006 - 04:50 PM

try using the stristr function

$file = file("path");

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

:: gucci.com ::
:: blockbuster.com ::

--------------------

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 July 2006 - 04:53 PM

http://www.webmaster...forum88/938.htm

exsplode
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 02 July 2006 - 05:26 PM

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.

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

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 Eckstra

Eckstra
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 July 2006 - 09:27 PM

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?





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users