Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help parsing artist,songname and image url from remote html

Status
Not open for further replies.

shobert09

Programmer
Sep 26, 2007
11
NL
Hi all there is a remote html page that has artist,songname and image url .
I want to get these infromation as variables when ever i pull that page .

i tried the following solution but it doesn't output the image url for me( .
could any one tell me what i am doing wrong and how to fix it.Also i want these information
as varibles inteasd of current output type.Thanks

Code:
<?PHP
$html = file_get_contents("./test.html");
//preg_match("/<img border=\"0\" src=\"(.*)\" border=\"0\">$/", $html, $urls);
//preg_match("<td width=\"100\" (.*)>(.*)</td>", $html, $tdmatch);
preg_match("/<td width=\"100\" (.*)>(.*)<\/td>/", $html, $tdmatch);   

echo "<pre>";
//var_dump($urls);
var_dump($tdmatch);
echo "</pre>";

?>


Note: the information that i want to extract are shown in bold.


Html of page that i want to pull every few seconds:
<table border=0 width="280" bgcolor="#FFFFFF" cellpadding=5><tr><td align="center" width="80">
<a href=" target="_blank"><img border="0" src=" border="0"></a></td><td width="100" style="font-family:Arial; font-size:12px; color:#000000;">the power of love<br>"celine dion"</td><td valign="top"><table><tr><td><table><tr><td><a href=" target="_blank"><img border="0" src=" width="30" height="30" border="0"></a></td><td><font size="1" face="Arial" color="red"><b>RADIO<br> </b></font></td></tr></table></td></tr><tr><td><a href=" target="_blank"><img border="0" src=" href=" target="_blank"><img border="0" src="

current output image attached:

 
i would use regular expressions to extract these tags.

you could use this pattern to extract the urls of anchor tags. an easy adaptation can be made for img tags.

Code:
//this pattern
$pattern = '/<a.?*\bhref\\s*=\\s*(\'|")(.*?)\\1/i';
preg_match($pattern, $websitecontents, $matches);
print_r($matches);
 
jpadie thanks for reply. I tried you solution but i keep getting the following error. could you also tell me how to get image tag,song name,artist name using this method as variables?Thanks

Code:
Warning: preg_match() [function.preg-match]: Compilation failed: reference to non-existent subpattern at offset 4 in fetchtest2.php on line 5
Array ( )

Code:
<?PHP
$html = file_get_contents("./test.html");
//this pattern
$pattern = '/<a.?*\bhref\\s*=\\s*(\'|")(.*?)\\1/i';
preg_match($pattern, $websitecontents, $matches);
print_r($matches);

?>
 
try this pattern

Code:
$pattern = '<a.*?\\bhref\\s*=\\s*(\'|")(.*?)\\1/i';

i did not test the previous code. mea culpa.
 
i tried it and got this error at first:

Code:
Warning: preg_match() [function.preg-match]: No ending matching delimiter '>' found in on line 6

Then i added > at the end of that line and i got this:
Array()
Code:
$pattern = '<a.*?\\bhref\\s*=\\s*(\'|")(.*?)\\1/i>';

Furthemore, do you think this will out put image url,songname and artist name all together?
 
put a forward slash at the beginning.

or, if you want more help, give me the url for the website your querying so that i can test it.
 
jpadie do you mean i need to put \ or / at :

Code:
$pattern = '/<a.*?\\bhref\\s*=\\s*(\'|")(.*?)\\1/i>';

The actual url is :


i want to get image url and song name and artis name as varibles not just displaying them. I be happy if you be able to help me with this.Thanks
 
that makes life easier.

here is the code you need. the variables are available in $imageURL, $artist and $song

Code:
<?php
    $url = '[URL unfurl="true"]http://services.radiojavan.com/rss.php?html=280';[/URL]
	$pattern ='/<img border="0" src="http:\/\/(.*?)" border="0"><\/a><\/td><td width="100" style="font-family:Arial; font-size:12px; color:#000000;">(.*?)<br>(.*?)<\/td>/i';
	preg_match($pattern, file_get_contents($url), $matches);
	list ($ignore, $imageURL, $artist, $song) = $matches;
?>
 
jpade thanks since i am using that image in the following code. is it possible to add http:\\ at start of image url since the follwoing code fails wihtout it:

Code:
$background = imagecreatefrompng("./backround_image3.png");

  
        $insert = imagecreatefromjpeg(str_replace(' ', '%20', $imageURL));

  	imagecopymerge($background,$insert,47,50,0,0,80,80,100);

	$fontcolor = imagecolorallocate($background, 255, 255, 255);
	$font = 'font/arialbd.ttf';

i get this error:
Code:
<br />
<b>Warning</b>:  imagecreatefromjpeg([URL unfurl="true"]www.radiojavan.com/static/artists/shahab%20tiam.jpg):[/URL] failed to open stream: No such file or directory in <b>/home/[URL unfurl="true"]www/Display/current2.php</b>[/URL] on line <b>35</b><br />
<br />
<b>Warning</b>:  imagecopymerge(): supplied argument is not a valid Image resource in <b>/home/[URL unfurl="true"]www/Display/current2.php</b>[/URL] on line <b>37</b><br />
‰PNG

 
you would just do this:

Code:
$insert = imagecreatefromjpeg('[URL unfurl="true"]http://'.$imageURL);[/URL]
 
jpadie thanks once agine. Buti want to keep the str_relace since some of the image names has spaces on it. how should i add that http:// to the following line and keeping str_replace?

Code:
$insert = imagecreatefromjpeg(str_replace(' ', '%20', $imageURL));
 
i still don't see why you need the str_replace. if you must, use urlencode instead.
Code:
$insert = imagecreatefromjpeg(urlencode('[URL unfurl="true"]http://'.$imageURL));[/URL]
 
jadie since some of the image urls have spaces and strange characters i used that str_replace reduce the problem.when the image url has space i get this error:


Code:
br />
<b>Warning</b>:  imagecreatefromjpeg([URL unfurl="true"]http://www.radiojavan.com/static/artists/parviz[/URL] rahman panah.jpg): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
 in <b>/home/[URL unfurl="true"]www/current2.php</b>[/URL] on line <b>36</b><br />
<br />
<b>Warning</b>:  imagecopymerge(): supplied argument is not a valid Image resource in <b>/home/[URL unfurl="true"]www/current2.php</b>[/URL] on line <b>38</b><br />
‰PNG


IHDR¯ÝBµè IDATxœì½y|^Gu7þ?«G’emÖnÉZ,˶,Çvâìqg!!qBRh)kmq€7amy_Â(]Ò6?Â%…RkZÀ†,^ÈfÇNìlŽ÷EÖbíÖ.Y²öçžßsgî™™ûÈr¡¿???

pointing at the following lines:

Code:
        $insert = imagecreatefromjpeg('[URL unfurl="true"]http://'.$imageurl);[/URL]
  	//$insert = imagecreatefromjpeg($imageurl);
  	imagecopymerge($background,$insert,47,50,0,0,80,80,100);

what i ma trying to take the image and add another image around it like frame but when image url has spaces or strange characters it fails and gives me that error
 
so use the urlencode code that i gave you in the last post.

if even that does not work, then try the raw_url_encode() equivalent.

personally, it works fine for me without the encoding.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top