How can i get image using preg_match_all - php

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

Related

How to replace a variable URL string (inside <img src="">) depending on pattern? (SimplePie)

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 .

Parsing image url from source code of the page

Here is my regex to get the image url on the page.
<?php
$url = $_POST['url'];
$data = file_get_contents($url);
$logo = get_logo($data);
function get_logo($html)
{
preg_match_all('/\bhttps?:\/\/\S+(?:png|jpg)\b/', $html, $matches);
//echo "mactch : $matches[0][0]";
return $matches[0][0];
}
?>
Is there any thing missing in regex? for some of the url it does not give image url though they have image in it.
for example: http://www.milanart.in/
it does not give image on that page.
Please No dome. I could not use it.
<?php
$url = "http://www.milanart.in";
$data = file_get_contents($url);
$logo = get_logo($data);
function get_logo($html)
{
preg_match_all("/<img src=\"(.*?)\"/", $html, $matches);
return $matches[1][0];
}
echo 'logo path : '.$logo;
echo '<img src="'.$url.'/'.$logo.'" />';
?>
Use DOM Class of PHP to get all images:
Search for image files in CSS.....url(imagefilename.extension)
Search for image file in HTML ......

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;

how to get link from img tag

$img = '<img src="http://some-img-link" alt="some-img-alt"/>';
$src = preg_match('/<img src=\"(.*?)\">/', $img);
echo $src;
I want to get the src value from the img tag and maybe the alt value
Assuming you are always getting the img html as you shown in the question.
Now in the regular expression you provided its saying that, after the src attribute its given the closing tag for img. But in the string there is an alt attribute also. So you need to care about it also.
/<img src=\"(.*?)\".*\/>/
And if you are going to check alt also then the regular expression.
/<img src=\"(.*?)\"\s*alt=\"(.*?)\"\/>/
Also you are just checking whether its matched or not. If you need to get the matches, you need to provide a third parameter to preg_match which will fill with the matches.
$img = '<img src="http://some-img-link" alt="some-img-alt"/>';
$src = preg_match('/<img src=\"(.*?)\"\s*alt=\"(.*?)\"\/>/', $img, $results);
var_dump($results);
Note : The regex given above is not so generic one, if you could provide the img strings which will occur, will provide more strong regex.
function scrapeImage($text) {
$pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/';
preg_match($pattern, $text, $link);
$link = $link[1];
$link = urldecode($link);
return $link;
}
Tested Code :
$ input=’<img src= ”http://www.site.com/file.png” > ‘;
preg_match(“<img.*?src=[\"\"'](?<url>.*?)[\"\"'].*?>”,$input,$output);
echo $output; // output = http://www.site.com/file/png
How to extract img src, title and alt from html using php?
See the first answer on this post.
You are going to use preg_match, just in a slightly different way.
Try this code:
<?php
$doc = new DOMDocument();
$doc->loadHTML('<img src="" />');
$imageTags = $doc->getElementsByTagName('img');
foreach($imageTags as $tag) {
echo $tag->getAttribute('src');
}
?>
Also you could use this library: SimpleHtmlDom
<?php
$html = new simple_html_dom();
$html->load('<html><body><img src="image/profile.jpg" alt="profile image" /></body></html>');
$imgs = $html->find('img');
foreach($imgs as $img)
print($img->src);
?>
preg_match('/<img src=\("|')([^\"]+)\("|')[^\>]?>/', $img);
You already have good enough responses above, but here is another one code (more universal):
function retrieve_img_src($img) {
if (preg_match('/<img(\s+?)([^>]*?)src=(\"|\')([^>\\3]*?)\\3([^>]*?)>/is', $img, $m) && isset($m[4]))
return $m[4];
return false;
}
You can use JQuery to get src and alt attributes
include jquery in header
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
//HTML
//to get src and alt attributes
<script type='text/javascript'>
// src attribute of first image with id =imgId
var src1= $('#imgId1').attr('src');
var alt1= $('#imgId1').attr('alt');
var src2= $('#imgId2').attr('src');
var alt2= $('#imgId2').attr('alt');
</script>

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