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)$/;
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
I am working on some code and I've done enough work to get something going. I want to replace a image url(s) and web links within that body of text.
E.G "This is my text with http://www.google.com and some image http://www.somewebimage.png"
Replace to "This is my text with http://www.google.com and some image <img src="http://www.somewebimage.png">"
My hack gets me to replace the url(s) or the img links but not both..one is over written because of the order
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$reg_exImg = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?(jpg|png|gif|jpeg)/';
$post = "This is my text with http://www.google.com and some image http://www.somewebimage.png";
if(preg_match($reg_exImg, $post, $img)) {
$img_post = preg_replace($reg_exImg, "<img src=".$img[0]." width='300' style='float: right;'> ", $post);
} else {
$img_post = $post;
}
if(preg_match($reg_exUrl, $post, $url)) {
$img_post = preg_replace($reg_exUrl, "<a href=".$url[0]." target='_blank'>{$url[0]}</a> ", $post);
} else {
$img_post = $post;
}
If I block the $reg_exUrl code block I get the image link if it runs the I get the url link.
You can do it in a single pass, your two patterns are very similar and it's easy to build a pattern that handles the two cases. Using preg_replace_callback, you can choose the replacement string in the callback function:
$post = "This is my text with http://www.google.com and some image http://www.domain.com/somewebimage.png";
# the pattern is very basic and can be improved to handle more complicated URLs
$pattern = '~\b(?:ht|f)tps?://[a-z0-9.-]+\.[a-z]{2,3}(?:/\S*)?~i';
$imgExt = ['.png', '.gif', '.jpg', '.jpeg'];
$callback = function ($m) use ($imgExt) {
if ( false === $extension = parse_url($m[0], PHP_URL_PATH) )
return $m[0];
$extension = strtolower(strrchr($extension, '.'));
if ( in_array($extension, $imgExt) )
return '<img src="' . $m[0] . '" width="300" style="float: right;">';
# better to do that via a css rule --^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
return '' . $m[0] . '';
};
$result = preg_replace_callback($pattern, $callback, $post);
What I want
If the URL in the string contains a .jpg at the end of the URL (not the string) then it should make an image from it with preg_replace else make a normal link.
so for example:
If I have http://www.example.com/images/photo.jpg then it should replace with:
<img src="http://www.example.com/images/photo.jpg" alt="http://www.example.com/images/photo.jpg">
The problem:
The URL is replaced with a link in any way and my regex isn't working :( .
What I have tried:
$content = preg_replace("/(http:\/\/[^\s]+(?=\.jpg))/i","<img src=\"$1\" alt = \"$1\"></img>",$content);
$content = nl2br(preg_replace("/(http:\/\/[^\s]+(?!\.jpg))/m", "$1", $content));
Try this
function replace_links($content)
{
if (preg_match('#(http://[^\s]+(?=\.(jpe?g|png|gif)))#i', $content))
{
$content = preg_replace('#(http://[^\s]+(?=\.(jpe?g|png|gif)))(\.(jpe?g|png|gif))#i', '<img src="$1.$2" alt="$1.$2" />', $content);
}
else
{
$content = preg_replace('#(http://[^\s]+(?!\.(jpe?g|png|gif)))#i', '$1', $content);
}
return $content;
}
$content = preg_replace('#\b(http://\S+\.jpg)\b#i', '<img src="$1" alt="$1" />', $content);
You don't need lookaround. Just go with
$content = preg_replace("#(http://[^ ]+\\.jpg(?= |$)#i","<img src=\"$1\" alt=\"$1\"/>", $content);
I think you used the lookahead operator when you wanted lookbehind. You could change (?=\.jpg) to (?<=\.jpg) but there are other, cleaner regex's I'm sure others will post.
This worked for me.
$parse_img='Hello, http://orbitco-ccna-pastquestions.com/images/Q5.jpg
In the figure above, router R1 has two point-to-point . ';
$parse_img=preg_replace('/(https?:\/\/(.\*)?\\.jpg|png|gif)[\s+]*/i',"< img src=\"$1\" alt = \"$1\">< /img >",$parse_img);
echo $parse_img;
Suyash
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 ......
$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>