preg_replace - turn <img> into <a href....><img</a> - html

I'm struggling to turn an image tag into a link and copy parameters within the tag i.e.
<img src="/images/image1.jpg" alt="The name of image">
into
<img src="/images/image1.jpg" alt="The name of image">
My problem is not just duplicating the src and alt data but account for missing and extra tags i.e.
<img src="/images/image1.jpg" >
into
<img src="/images/image1.jpg" >
and
<img style="padding:5px;" src="/images/image1.jpg" alt="The name of the image">
into
<img style="padding:5px;" src="/images/image1.jpg" alt="The name of the image">
This needs to be done for all instances of the img tag throughout a string.
Not meaning to sound like a challenge but can anyone propose a possible solution, I'm sure this can be done with preg_replace but I just can't work it through?
Thank you

The simplest way to do that:
HTML:
<span id="My_ID"><img class="My_img" src="/images/image1.jpg" alt="The name of image" /></span>
JavaScript and JQUERY(Not necessary but i used it for browser compatibility):
var first= true;
$('.My_img').hover(function(){
if(first == true){
//First time this prevent Dulicated runing and shows errors
first = false;//it is not thew first time anymore
var alt = $('.My_img').attr('alt');//Get the img alt
var src = $('.My_img').attr('src');//Get the img src
document.getElementById('My_ID').innerHTML = '';
}
});
To demostrate the above her is a fiddle:
http://jsfiddle.net/Mh9Np/
Simply but innerHTML is a bad practice as everyone say so i will give you an idea about the other way:(Samse HTML)
1-Store the img in a variable
2-Store the alt and src attribute in variables.
2-Create the a element.
3-Set to it the attributes you want with the help of alt and src variables.
4-Append the img to a.
5-Append a to the span.
6-Delete the original img.//Optional it might help resolving some styling issues.

Related

ASP NET MVC href to an image?

I have the following razor syntax to get the picture from controller (From database table):
<img src="#Url.Action("GetMainPicture", "Product", new { item.ProductID, #class = "img-responsive" })" height="300" width="230" alt="" />
Which works just fine i can get the images in <img> tags as you can see in above code.
Now in the template i downloaded it for my project i have this here below, which is a href styled with a button to zoom the image with fancy box:
Zoom
The code line above works if i give a straight forward path for the image (path for the image within a folder or something not database) but i can't get it working fetching the image from database and then show it on a fancy box which fancier.
Thank You!
EDIT: Not to forget the images are in the database table.
You have to put the full url there, without ../ I think
but now I would like to have a href (link) to that specific picture
how can I do it?
You just need to wrap your image inside a anchor tag.
<a href="someURL">
<img src="#Url.Action("GetMainPicture", "Product", new { item.ProductID, #class = "img-responsive" })" height="300" width="230" alt="" />
</a>
You can do like below. Set the href attribute of anchor tag (<a>) to where you want or what the operation you want to perform when user clicks on the picture.
<a href="#Url.Action("GetMainPicture", "Product", new { item.ProductID})">
<img src="#Url.Action("GetMainPicture", "Product", new { item.ProductID, #class = "img-responsive" })" height="300" width="230" alt="" />
</a>
<a href="#Url.Action("GetMainPicture", "Product", new { item.ProductID)">
<img src="#Url.Content(#item.image)" class="img-responsive " style="height="300" width="230"" alt="#item.ContName" />
</a>
i have used this approach to take the client to a new form where he gets all the detail about the product and this is working for me so it may help you
Use the image link and the Url.Content in razor like this example:
<img src="#Url.Content(item.Image)" alt="MyImage" />
or:
Load Image

Can I display Image in alt attribute if an image fails to load

<img src="#" alt="Sorry the image could not be displayed.">
In the above code I want display an image in alt instead of text.In this way I want to display my site logo as image if it is unable to connect to internet.
Update:I am developing android application using Phonegap.
As stated in the comments - If your user doesn't have an internet connection, there's no way they can load a new image. However, if it's of any use to you and you wanted to load in placeholders for whatever reason.
You could do something like this with jQuery
I've set up a basic fiddle for you which is easy enough to understand.
Image With # Src
<img src="#" alt="no image" />
Variables to find # Src and to replace with placeholder
var noSrc = '#';
var noImg = 'http://placehold.it/400x500';
$('img[src="' + noSrc + '"]').attr('src', noImg);
Just change the path of the noImg variable to whatever your image path will be.
Working Example: Fiddle
<img src="#" alt="no image" />
$('img[src="#"]').attr('src', 'http://placehold.it/400x500');
Example without Variables: Fiddle 2

How to avoid img error icon

If you give an src and it cannot find the img it usually appears a square with a X. Is there any way to avoid that? I mean if it cannot find the img, just show nothing
You can check your solution here:http://jsfiddle.net/GmBax/
HTML:
<img src="1.png" width="42" height="42" >
There's another option, you can load a default empty image on error. Ex: <img src="/img.jpg" onerror="this.src='/none.jpg'" />
put in an "alt" and a "title" tag - this way if the image doesn't show, the alt text will show instead.
e.g.
<img src="1.png" width="42" height="42" alt="hello" title="hi">
Use the alt parameter for example.. You can either add a text or leave it empty.
<img src="1.png" width="42" height="42" alt="" />
Or handle it by some PHP or whatever you're using.

image attributes appearing priority

<img src="#" alt="abc"/>
after above code rendered in browser the position of alt and src are changed like
<img alt="abc" src="#"/>
Is there is any way to fix these problem
"I want that every time src comes before others attributes!"

How do I solve this issue in IE7?

When I am testing my page in IE7, all the image have a tooltip corresponding to the text of the alt in the image tag..
<img src="Myimage.jpg" alt="No pic" />
When I hover my mouse on the displayed pic in IE7, I get a tooltip "No pic" corresponding to the text of the alt .How do I fix this ?
IE6/7 treats the alt attribute as though it was a title attribute - but only if there's no actual title attribute set.
You can workaround it with a blank title:
<img src="Myimage.jpg" alt="No pic" title="" />
You can try adding an empty title tag
<img src="image.jpg" alt="nopic" title="" />
The answer has been posted already (empty title tag).
However, (in reference to one of the answers) alts are supposed to describe the image for 508 compliance reasons and if the image doesn't show up, so you should change the alt text to describe your picture.
I would have made a comment on the original post but SO doesn't allow me to yet.
The actual question here is why are you using alt the way you are? If your image is simply decorative, you should have an empty alt attribute. look at the W3CS definition of the alt attribute.
http://www.w3.org/QA/Tips/altAttribute
On this occasion is suspect youd actually want:
<img src="Myimage.jpg" alt="" />