Jump to content

how to read source (some lines) using PHP ?


Soledad

Recommended Posts

hello my friends :)for example I have this link : http://ara.reuters.com/article/topNews/idA...E7750DM20110806I've tried so much to get the url of image from its page source using preg_match but failed :)the image url is included in image_srcI was able to get short text from <span id="midArticle_start"> but failed to get the image ....please help me to do that .......thanks

Link to comment
Share on other sites

this is my code

$url = "http://ara.reuters.com/article/topNews/idARACAE7750DM20110806";$contents = file_get_contents($url);$pattern = '/<head>(.*?)<\/head>/s';preg_match($pattern, $contents, $match);	$contents = $match[0]; 														 $pattern = '/<link rel="image_src" href="(.*?)" \/>/s';  preg_match_all($pattern, $contents, $match);  echo $match[0];

Link to comment
Share on other sites

the image url I wanna get is :http://ARA.reuters.com/resources/r/?m=02&a...SOMAL-REBEL-AT1
<link rel="image_src" href="http://ARA.reuters.com/resources/r/?m=02&d=20110806&t=2&i=474059649&w=130&fh=&fw=&ll=&pl=&r=2011-08-06T173005Z_01_ACAE7751CMF00_RTROPTP_0_OEGWD-SOMAL-REBEL-AT1" />

but sorry I didn't understand well what do you mean ??

he asking why you are doing this
$pattern = '/<head>(.*?)<\/head>/s';preg_match($pattern, $contents, $match);	$contents = $match[0];

AND this

$pattern = '/<link rel="image_src" href="(.*?)" \/>/s';  preg_match_all($pattern, $contents, $match); echo $match[0];

and also wants you to know what the output of $contents is here:

$pattern = '/<head>(.*?)<\/head>/s';preg_match($pattern, $contents, $match);	$contents = $match[0];

Link to comment
Share on other sites

This uses regular expressions. It's less elegant (I think) but it seems to work.
$url = "http://ara.reuters.com/news/archive/topNews?date=today";$base = "http://ara.reuters.com/news/archive";$data = file_get_contents($url);$re = '/primaryContent.*standalone/s';preg_match($re, $data, $matches);$data = $matches[0];$re = '/<a href.*?"(.*?)".*?>(.*?)<\/a>/s';preg_match_all($re, $data, $matches);foreach ($matches[1] as $link) {	echo $base . $link . "\n";}foreach ($matches[2] as $text) {	echo $text . "\n";}

The echo statements are just for example. The important thing is that your partial URLs are in the $matches[1] sub-array, and the corresponding texts are in the $matches[2] sub-array.

Deirdre's Dad helped me to start with my rss feeds with this form and I used for the similar issues
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...