Formatting xml rest return - php

I created php code that gets a rest xml response. The code follows:
<table valign="top" cellpadding="10" style=" padding:0px 15px;"><tr></tr><tr>
<?php $song = str_replace(' ', '+', $post['title']);
$url = "http://ws.audioscrobbler.com/2.0/?method=chart.gettoptracks&limit=5&api_key=*************";
$xml=simplexml_load_file("$url");
foreach($xml->tracks->track as $track){
foreach($track->name as $name) {
$image = $track->image;
$link = str_replace(' ', '-', $name);
echo "<th width='20%' valign='top'><img src='$image' alt='$name' height='90' width='90'></br></br><a href='http://www.mp3jive.com/music/download/1/$link.html'>$name</a></th>";
}
}
?>
</tr>
</table>
It retrieves xml such as the following:
<lfm status="ok">
<tracks page="1" perPage="5" totalPages="200" total="1000">
<track><name>Chandelier</name><duration>216</duration><playcount>109163</playcount><listeners>38691</listeners><mbid>4d507ca4-0d95-494c-b97b-e2fa50c926c1</mbid><url>http://www.last.fm/music/Sia/_/Chandelier</url><streamable fulltrack="0">0</streamable><artist><name>Sia</name><mbid>2f548675-008d-4332-876c-108b0c7ab9c5</mbid><url>http://www.last.fm/music/Sia</url></artist><image size="small">http://userserve-ak.last.fm/serve/34s/98738409.png</image><image size="medium">http://userserve-ak.last.fm/serve/64s/98738409.png</image><image size="large">http://userserve-ak.last.fm/serve/126/98738409.png</image><image size="extralarge">http://userserve-ak.last.fm/serve/300x300/98738409.png</image></track>
<track><name>Do I Wanna Know?</name><duration>273</duration><playcount>64234</playcount><listeners>33378</listeners><mbid>575d24ee-cf80-48ad-beeb-9295c7d05b35</mbid><url>http://www.last.fm/music/Arctic+Monkeys/_/Do+I+Wanna+Know%3F</url><streamable fulltrack="0">0</streamable><artist><name>Arctic Monkeys</name><mbid>ada7a83c-e3e1-40f1-93f9-3e73dbc9298a</mbid><url>http://www.last.fm/music/Arctic+Monkeys</url></artist><image size="small">http://userserve-ak.last.fm/serve/34s/92744747.png</image><image size="medium">http://userserve-ak.last.fm/serve/64s/92744747.png</image><image size="large">http://userserve-ak.last.fm/serve/126/92744747.png</image><image size="extralarge">http://userserve-ak.last.fm/serve/300x300/92744747.png</image></track>
<track><name>A Sky Full of Stars</name><duration>268</duration><playcount>60604</playcount><listeners>30131</listeners><mbid>d0d9e6e7-47c1-4470-9124-d10613c09ece</mbid><url>http://www.last.fm/music/Coldplay/_/A+Sky+Full+of+Stars</url><streamable fulltrack="0">0</streamable><artist><name>Coldplay</name><mbid>cc197bad-dc9c-440d-a5b5-d52ba2e14234</mbid><url>http://www.last.fm/music/Coldplay</url></artist><image size="small">http://userserve-ak.last.fm/serve/34s/99319661.png</image><image size="medium">http://userserve-ak.last.fm/serve/64s/99319661.png</image><image size="large">http://userserve-ak.last.fm/serve/126/99319661.png</image><image size="extralarge">http://userserve-ak.last.fm/serve/300x300/99319661.png</image></track>
<track><name>Stay with Me</name><duration>173</duration><playcount>70310</playcount><listeners>30071</listeners><mbid>c8dbf2c6-cbd2-48d2-ae04-e3098e29e306</mbid><url>http://www.last.fm/music/Sam+Smith/_/Stay+with+Me</url><streamable fulltrack="0">0</streamable><artist><name>Sam Smith</name><mbid>5a85c140-dcf9-4dd2-b2c8-aff0471549f3</mbid><url>http://www.last.fm/music/Sam+Smith</url></artist><image size="small">http://userserve-ak.last.fm/serve/34s/99230175.png</image><image size="medium">http://userserve-ak.last.fm/serve/64s/99230175.png</image><image size="large">http://userserve-ak.last.fm/serve/126/99230175.png</image><image size="extralarge">http://userserve-ak.last.fm/serve/300x300/99230175.png</image></track>
<track><name>Summer</name><duration>224</duration><playcount>57584</playcount><listeners>29519</listeners><mbid>9947d625-1d29-45df-a988-6e907fd1fea1</mbid><url>http://www.last.fm/music/Calvin+Harris/_/Summer</url><streamable fulltrack="0">0</streamable><artist><name>Calvin Harris</name><mbid>8dd98bdc-80ec-4e93-8509-2f46bafc09a7</mbid><url>http://www.last.fm/music/Calvin+Harris</url></artist><image size="small">http://userserve-ak.last.fm/serve/34s/97561221.png</image><image size="medium">http://userserve-ak.last.fm/serve/64s/97561221.png</image><image size="large">http://userserve-ak.last.fm/serve/126/97561221.png</image><image size="extralarge">http://userserve-ak.last.fm/serve/300x300/97561221.png</image></track>
</tracks>
</lfm>
This code when grabbing the image gets the small image. I would like it to grab the large image. How can I easily alter it to do so. Thanks.

Since the large file is always the 3rd, you could simply put:
foreach($xml->tracks->track as $track){
foreach($track->name as $name) {
$image = $track->image[2]; // it is the third image
$link = str_replace(' ', '-', $name);
echo "<th width='20%' valign='top'><img src='$image' alt='$name' height='90' width='90'></br></br><a href='http://www.mp3jive.com/music/download/1/$link.html'>$name</a></th>";
}
}

The quickest solution, access the image by its index:
$image = $track->image[2]; // large
$image = $track->image[3]; // extralarge

Related

PHP : Getting image by removing all classes, ids ..ect from source

I have a image in database stored in this format
$link = this is my photo <img data-liked='0' data-reblogged='0' data-attachment-id="299971" data-medium-file="http://my.files.wordpress.com/2013/12/img_6697.png?w=394" width="73" height="130" src="http://my.files.wordpress.com/2013/12/img_6697.png?w=73&h=130" class="attachment-thumbnail" alt="IMG_6697" />, school photo;
i was wondring if this is possible to just get output
this is my photo <img src="http://my.files.wordpress.com/2013/12/img_6697.png?w=73&h=130" />, school photo
Just image source nothing else ...
I have tryed this and got image url but i need above result
$str = preg_replace('#<img.+?src=[\'"]([^\'"]+)[\'"].+/>#i', "$1", $link);
echo $str;
$str = preg_replace('#<img.*?src="(.*?)".*?\/>#i', "<img src=\"$1\" />", $link);
echo $str;
You might want to keep the alt:
$str = preg_replace('#<img.*?src="(.*?)".*?alt="(.*?)".*?\/>#i', "<img src=\"$1\" alt=\"$2\" />", $link);
echo $str;

Php - How can i print a image using printf?

Im trying to print a image using printf, but it doesnt work.
The column "imagem" show the directory of the image.
function listaCDs() {
$sql = "SELECT * FROM `cds`";
if(($rs=$this->bd->executarSQL($sql))){
while ($row = mysql_fetch_array($rs)) {
printf("
<img src=%s height="100" width="100" />", $row['imagem']);
echo "<br>";
}
}
else {
return false;
}
}
Try:
printf("<img src='%s' height='100' width='100' />", $row['imagem']);
Use this
printf("<img src=%s height='100' width='100' />", $row['imagem']);
instead of
printf("<img src=%s height="100" width="100" />", $row['imagem']);
You have double quotes inside double quotes for attributes, which is the problem. Check this demo.
Make sure the image's directory is public (i.e copy and paste $row['imagem'] in a browser to see if it's accessible).

php echo add text end of the line

foreach($divs as $element)
{
$img = $element->find('a', 0)->href.'<br>';
$img = str_replace("http://www.youtube.com/watch?v=", "http://img.youtube.com/vi/", $img);
echo $img."/mqdefault.jpg";
}
Now output is :
http://img.youtube.com/vi/UYkHcfrxxtk
/mqdefault.jpghttp://img.youtube.com/vi/ZgjaveCUsSg
/mqdefault.jpghttp://img.youtube.com/vi/TFVbehwl1Ns
/mqdefault.jpg
I want like this each img:
http://img.youtube.com/vi/UYkHcfrxxtk/mqdefault.jpg
How can get fix this? i used simple html dom.
echo $img."/mqdefault.jpg<br/>";
But if you are trying to make image elements on a page:
echo "<img src='".$img."' /><br/>";
use this.
remove .'<br>' from end of $img = $element->find('a', 0)->href.'<br>'; then add it to echo $img."/mqdefault.jpg";
<br> in html mean newline
$img = $element->find('a', 0)->href;
$img = str_replace("http://www.youtube.com/watch?v=", "http://img.youtube.com/vi/", $img);
echo $img."/mqdefault.jpg".'<br>';

PHP dynamically change filename

I was able to get some help earlier today, but I didn't have everything from the original script for it to work. Basically I have a list of image file names in a .txt file. They each load in a slideshow, and change with the pagination on the page.
What I would like to do, is if I have a file that has a .mov extension, for example, the php script will load a movie player instead.
Here is the original slideshow script
<div id='jessslide'>
<?php
echo"
<div id='slider-wrapper'>
<div id='slider' class='nivoSlider'>";
$photos = file("work.txt");
foreach ($photos as $image) {
$item = explode("|", $image);
if ($item[0] == $fields[0]) {
$photo = trim($item[1]);
echo"<img src='images/work/$photo' alt='' />\n";
}
}
echo"
</div></div>"
?>
</div>
And here is my bad attempt at trying to make this work...
<div id='jessslide'>
<?php
$photos = file("work.txt");
$img = array('jpg', 'png', 'gif');
$vid = array('swf', 'mp4', 'mov', 'mpg', 'flv');
foreach ($photos as $image) {
$item = explode("|", $image);
if ($item[0] == $fields[0]) {
$photo = trim($item[1]);
$ext = explode(".", $image);
if (in_array($ext[1], $img))
{
echo "<div id='slider-wrapper'><div id='slider' class='nivoSlider'><img src='images/work/$photo' alt='' /> </div></div>";
}
elseif (in_array($ext[1], $vid))
{
echo "<iframe src='$photo' width='800' height='450' frameborder='0' webkitAllowFullScreen allowFullScreen></iframe>";
}
}
}
?>
</div>
I would really appreciate if someone could help me out to finally bring this script to life! :)
The most likely problem I see is the possibility that the assignment to $ext should be tied to $item or $photo instead of $image. If that solves it, then great. Otherwise, read below for a more complete analysis and some suggestions for steps to debug until you narrow in on the cause of the problem.
Assuming all of the data is accurate, the script you've written looks like it should work. I've reformatted it here to conduct some analysis:
<div id='jessslide'>
<?php
$photos=file("work.txt");
$img = array('jpg', 'png', 'gif');
$vid = array('swf', 'mp4', 'mov', 'mpg', 'flv');
foreach($photos as $image){
$item=explode("|",$image);
if($item[0]==$fields[0]){
$photo=trim($item[1]);
$ext = explode(".", $image);
if(in_array($ext[1], $img))
{ echo "<div id='slider-wrapper'><div id='slider' class='nivoSlider'><img src='images/work/$photo' alt='' /> </div></div>"; }
elseif(in_array($ext[1], $vid))
{ echo "<iframe src='$photo' width='800' height='450' frameborder='0' webkitAllowFullScreen allowFullScreen></iframe>"; }
}
}
?>
</div>
There are three major places in this script for problems to occur that would cause nothing to be output.
The first is in the foreach() loop. If the $photos array has nothing in it, you would skip the entire block of code. You can test for this condition by adding a print_r($photos); before the foreach() and then as the first line in the body of the foreach() add echo $image." "; to verify that all the files are listed as you expect. If that looks correct, remove that debugging code and move on.
The 2nd potential for problems is if the $item[0] is not equal to $fields[0]. To test this, add echo 0; as the first line inside if($item[0]==$fields[0]). If you see zeros as expected when the script is run, then you can remove this debugging code and move on.
The 3rd potential for problems is pull/examination of the extension. One likely candidate for problems here is if the assignment to $ext should be tied to $item or $photo instead of $image but there are definitely other possible issues. To test this, add echo $image." ".$ext[1]."\n"; print_r($img); print_r($vid); before the if(in_array($ext[1], $img)). Then add a temporary else clause with the body of echo 3; as well. Verify that th
Once you figure out which condition in the code is causing problems, you will be well on the way toward solving it. My guess is that you'll get through the tests and either find an obvious mistake in one of the early sections that we are assuming works right, or you will end up with 3 being printed out a lot. In the later case, one possible issue could come from lower/upper-case differences, which could be solved via changing the $item assignment to $item=strtolower(explode("|", $image);

How to extract image names with PHP or regex?

I have the following data in MySQL.
<p><img src="../../../../assets/images/frontpage/image1.png"
alt="" width="790" height="356" /></p>
Now I want to get image2.png with PHP or regex. The extension can be gif or jpg. And a length of image name can be any length.
This would match a path in an img tag and capture the file in the first interior capturing group.
<?php
if (preg_match('%<img\s.*?src=".*?/?([^/]+?(\.gif|\.png|\.jpg))"%s', $subject, $regs)) {
$image = $regs[1];
} else {
$image = "";
}
?>
This code should do what you need:
<?php
$regex = '#src[ ]*=[ ]*"[a-z/.]*/(.*?\.(?:png|gif|jpg))#i"';
$match = array();
if (preg_match($regex, $html, $match)) {
$imglocation = $match[1];
} else {
die('Failed to find image name.');
}
Try this:
$str = '<p><img src="../../../../assets/images/frontpage/image1.png"
alt="" width="790" height="356" /></p>';
$imageType = end(explode("."));
Use this expression: var expr=/[a-z]+\.(gif|jpg)$/;

Categories