1

I have a php code that prints some HTML stuff from MYSQL database.

Sometimes there are some img tags in there that don't have any width so I need to find those and add a width attribute via inline CSS to those img tags.

They look like this:

<img class="aligncenter size-medium wp-image-31kj" src="someimage.jpg" alt="">

<img class="alignleft large-medium wp-image-3154" src="someimage.jpg" alt="">

How can I find these img tags and add this to them:

style="width:100%"

Thanks in advance.

EDIT:

Already doing the above using jquery but its not reliable:

$(".img").each(function() {  
   var imgWidth = $(this).width();
   if (imgWidth == 0){
       $(this).css('width','100%');

   }
  });
6
  • Do this using javascript querySelectorAll('img').forEach() add your checks inside the foreach & apply the styling you want. Commented Jan 5, 2019 at 21:04
  • @ahmad, I am already using javascript but it fails sometimes and server side code is far more reliable than client side. Commented Jan 5, 2019 at 21:07
  • Pull the img with a parser, check for a width attribute, if not present add one. Commented Jan 5, 2019 at 21:08
  • Possible duplicate of How do you parse and process HTML/XML in PHP? Commented Jan 5, 2019 at 21:08
  • @user3783243 would a parser work with stuff that comes from MYSQL database?! Commented Jan 5, 2019 at 21:38

2 Answers 2

2

I would do this with simple, self-explanatory css code

img:not([width]) {
   width:100%;
}

Just target img tags that does not have width property

Sign up to request clarification or add additional context in comments.

3 Comments

This does not target images that does have only width set using inline CSS.
This actually works but its not PHP. how reliable is this method?
@JamesJuanjie Are you asking if you can rely on css? Pretty much.
0

Use querySelectorAll to get all img elements with class attribute containing wp-image and set the style.

document.querySelectorAll("[class*=wp-image]").forEach(function(img) {
  img.style.width = "100%";
});
<img src="https://duckduckgo.com/favicon.ico" class="wp-image-foo">
<img src="https://google.com/favicon.ico" class="wp-image-bar">


If you want to select all images without width attribute or CSS property, use this:

var imgs = document.getElementsByTagName("IMG");
for (var i = 0; i < imgs.length; i++) {
  var img = imgs[i];
  if (img.style.width == "" && img.attributes.width === undefined)
    img.style.width = "100%";
}
<img src="https://duckduckgo.com/favicon.ico" class="wp-image-foo">
<img src="https://google.com/favicon.ico" class="wp-image-bar">
<img width=50 src="https://stackoverflow.com/favicon.ico" class="wp-image-bar">

1 Comment

@JamesJuanjie please view edit in my answer (the second code snippet)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.