...

View Full Version : get remote content?



CodilX
06-20-2006, 05:29 PM
Hi everyone,

I'm new to the forums, and I hope you'll help me with the problem I have :)

I'm creating a vertical newsbar, and I'm trying to get content from a remote page.

I've tried writing the code with preg_match, but it just doesn't work for me :(

The page that I want to extract text from looks like this:


multiple lines of content
<img src="someimage.jpg">
multiple lines of content to be extracted
<div id="something">


Can somebody take a look at this?

thx - Matt

lavinpj1
06-20-2006, 05:38 PM
The code you are using would be quite handy to see.

~Phil~

CodilX
06-20-2006, 06:11 PM
<?php
$news= "http://www.link.com";
$handle = fopen($news, 'r');
while (!feof($handle))
{
$Data = fgets($handle, 256);
preg_match('/<img src="someimage.jpg">(.*?)<div id="something">/', $news, $matches);

echo '<pre>';
print_r($matches);
echo '</pre>';
}
}
?>

lavinpj1
06-20-2006, 06:18 PM
Well, normally you cannot fopen a remote url, it's best to use file_get_contents and I have no idea what this $Data is doing there.

I cannot test, as I don't have the real url, but this is what i'd do...


<?php
$news= file_get_contents("http://www.link.com");

preg_match('@(\<img src\="someimage.jpg"\>).*(?=\<div id\="something"\>)@', $news, $matches);

echo '<pre>';
print_r($matches);
echo '</pre>';
?>

Remember, you have to escape regex characters from your search string. They are . \ + * ? [ ^ ] $ ( ) { } = ! < > | : and are escaped with a \.

~Phil~

CodilX
06-20-2006, 06:23 PM
Thanks, but I get the following error:


Parse error: syntax error, unexpected T_STRING in host on line 4

lavinpj1
06-20-2006, 06:29 PM
Code executes fine here. What is your line 3, 4 and 5?

~Phil~

CodilX
06-20-2006, 06:31 PM
it's just the way you pasted it. and i just copied it :)

CodilX
06-20-2006, 06:33 PM
Now I get:



Array
(
)

:mad: :confused:

lavinpj1
06-20-2006, 06:35 PM
If you gave me the exact url, it'd be much easier to find the problem.

~Phil~

CodilX
06-20-2006, 06:37 PM
PM'ed

lavinpj1
06-20-2006, 06:38 PM
No PMs here

~Phil~

lavinpj1
06-20-2006, 06:39 PM
Oops, ignore that. Just took a while to come.

CodilX
06-20-2006, 08:22 PM
Any ideas?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum