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;
Related
i need some help for get images. Im using preg_match_all function.
Source: <img alt="test" title="test" src="/data/brands/test.png">
how can i get full image url ?
And this is my code for text. I need add image here.
<?
$link = 'link';
$marka = '#<div class="test">(.*?)</div>#si';
$getir = file_get_contents($link);
preg_match_all($marka,$getir,$test1);
$test = $test1[0];
echo $test[0]; ?>
Thanks.
This might help you
$str = '<img alt="test" title="test" src="/data/brands/test.png">';
$regex = '#src="(.+?)">#';
preg_match($regex,$str,$match);
echo $match[1];
//prints: /data/brands/test.png
So I'm trying to add smileys to my website (bbCodes) but I can't figure out how to do it. I have all the smileys triggerwords and outputs in my database to make it easier to remove/add smileys.
This bit of code below does nothing... I do not get an error and it doesn't replace for example :happy: with the image happy.png
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('/var/www/files/connect.php');
$SmileysQ = $DB->query("SELECT * FROM smileys");
$SmileysQ->setFetchMode(PDO::FETCH_ASSOC);
while($Smileys = $SmileysQ->fetch()) {
$text = preg_replace ('/\''.$Smileys['trigger'].'/is', '<img src="images/smileys/'.$Smileys['output'].'.png" height="15" width="15" />', $text);
}
What have I done wrong?
I think you have an unintended ' mark in the first argument of the preg_replace causing it to fail as it was searching for ':happy: not :happy:
The proper replace would more likely be:
$text = preg_replace ('/'.$Smileys['trigger'].'/is', '<img src="images/smileys/'.$Smileys['output'].'.png" height="15" width="15" />', $text);
example:
$text = ":happy: this is a test!";
$code = ":happy:";
$text = preg_replace ('/'.$code.'/is', '<img src="images/smileys/happy.png" height="15" width="15" />', $text);
print $text;
/*
outputs: <img src="images/smileys/happy.png" height="15" width="15" /> this is a test!
the extra ' gave me: :happy: this is a test!
*/
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
This is my first post here. I'm not sure if the title of my post is the most descriptive way of showing my problem, sorry in advance!
Anyway, I've got a Facebook RSS feed on my site using simplepie - I'm displaying posts with a brief summary of its content and an accompanying image, both coming from the feed itself.
I've got a single div that displays this information and is repeated according to the number of stories I've set to display, in my case 3, so the HTML page outputs three div's each with a different story/accompanying image.
<div class="block">
<a href="<?php echo $image_url ?>" target="_blank">
<img src="<?php echo $image ?>" alt="" />
</a>
</div>
The problem is Facebook RSS feeds only show tiny thumbnails generated from the original image (either external or from Facebook's server), but I want them in their original resolution.
I've succesfully replaced the thumbnails coming from Facebook's server by changing the name of the image (from "something_s.jpg" to "something_n.jpg"):
$image = str_replace("_s.jpg", "_n.jpg", $image);
I tried with ltrim to remove the Facebook URL appended before the external image's URL (e.g. https://fbexternal-a.akamaihd.net/safe_image.php?d=AQDZORAaPpbr5COr&w=154&h=154&url=http://example.com/image.jpg). So, I thought this would do it:
$image = ltrim(stristr(str_replace("_s.jpg", "_n.jpg", $image), '&url='), '&url=');
Oddly enough, external images showed up in their original resolution (e.g. example.com/image.jpg inside <img src="">) but those coming from Facebook (e.g. 10153970_10153145169383306_1966565627_n.jpg) did not anymore. The img src attribute is just empty!
What am I doing wrong here? Any help is appreciated.
(BTW, this is simplepie code in my page):
<?php
require_once('../php/autoloader.php');
$feed = new SimplePie();
$feed->set_feed_url(array('https://www.facebook.com/feeds/page.php?format=rss20&id=40796308305'));
$feed->set_item_limit(3);
$feed->init();
function returnImage ($text) {
$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
$pattern = "/<img[^>]+\>/i";
preg_match($pattern, $text, $matches);
$text = $matches[0];
return $text;
}
function scrapeImage($text) {
$pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/';
preg_match($pattern, $text, $link);
$link = $link[1];
$link = urldecode($link);
return $link;
}
$feed->strip_htmltags(array('embed','center','strong'));
$feed->handle_content_type();
foreach ($feed->get_items() as $item):
$feedDescription = $item->get_content();
$image = returnImage($feedDescription);
$image = scrapeImage($image);
$image = ltrim(stristr(str_replace("_s.jpg", "_n.jpg", $image), '&url='), '&url=');
$image_url= $item->get_permalink();
$description = $item->get_description();
$description = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', '', $description);
endforeach;
?>
Mr.Rod
use this custom function to change Image src Attribue..
<?php
function ChangeImageSrc($image , $Newsrc){
$regex = '#src="(([^"/]*/?[^".]*\.[^"]*)"([^>]*)>)#';
$Content = preg_replace($regex, $image, 'src="'.$Newsrc.'"');
return $Content;
}
// Useage
$markup = "<img src='fb.com/blahblah.jpg'>";
// Change Src
$toNewSrc = ChangeImageSrc($markup , 'http://example.com/newsrc.png');
print $toNewSrc;
?>
after adding this function to your script
change this line
$image = ltrim(stristr(str_replace("_s.jpg", "_n.jpg", $image), '&url='), '&url=');
to
$image = ChangeImageSrc('src="_n.jpg"', '_s.jpg');
you have to more customize this function to match your needs .
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).