I need to add an alt image tag to hundreds of images. The problem is that it will take a long time, and in any case, it seems to be a simpler solution.
I managed to achieve this using javascript as follows:
<script type='text/javascript'> //<![CDATA[ $(document).ready(function() { $('img').each(function(){ var $img = $(this); var filename = $img.attr('src') if (typeof attr == typeof undefined || attr == false) { $img.attr('alt', filename.substring((filename.lastIndexOf('/'))+1, filename.lastIndexOf('.'))); } }); }); //]]> </script>
What this does is exactly what I want, for example, if I have this image:
<img src="http://mywebsite.com/images/the-image1.jpg" />
then javascript will automatically add alt like this:
<img src="http://mywebsite.com/images/the-image1.jpg" alt="the-image1" />
Well, this is all fine and dandy, but now I want to do it with PHP instead, because the problem is that it is only adding tags to the interface, which means that it does not appear from the page source (only a validation element), which means that the search engine will not see the alt label, which means that the only way to do this is to translate it directly to the page using php.
So, how can I execute my javascript solution above using php?
source share