Jump to content

How to find only inline image tags?


joshis

Recommended Posts

I have some html content. I would like to find all the 'inline' images in the content. I dont need to find the images with in any other tags like anchor or div.

 

eg:

some content
<img src="image1.jpg" alt="" width="100" height="150" /> some content. <div><img src="image2.jpg" alt="" width="100" height="150" /></div> some content
<img src="image3.jpg" alt="" width="100" height="150" /> some content..<div><a href="#"><img src="image4.jpg" alt="" width="100" height="150" /></a></div> some content

 

I want to find img tags

 

<img src="image1.jpg" alt="" width="100" height="150" /> and <img src="image3.jpg" alt="" width="100" height="150" />

 

because they have no outer tags.

Link to comment
Share on other sites

Your best option is probably using some sort of document model such as DOMDocument, SimpleXML or XPath. Despite knowing of their existance I have to admit I've never used any of them, but I'm fairly confident they can grab tags without 'closing' tags. An alternative is possibly to use Regular Expressions, off the top of my head a pattern as simple as the following should suffice...

 

preg_match_all("#<img[^>]>#i", $input, $out);

 

Disclaimer: Untested.

Link to comment
Share on other sites

Well alas I'm not a badged member, which means my psychic powers aren't fully developed yet. If you actually want any more help you will have to give more details. But it probably doesn't help I forgot to finish the pattern...

 

preg_match_all("#<img[^>]*>#i", $input, $out);

Link to comment
Share on other sites

My actual requirement is,

 

My html contains many img tags. Some are inline and some are enclosed in a div or anchor tag. So my inline images are scattered in the page regardless of the style and size. So I would like to enclose these tags in a <div><a> wrap to style it. Thats why I need this pattern.

Link to comment
Share on other sites

That's all very well and good but you still didn't say how you'd used the code I'd provided nor in what way it wasn't working. At least you did provide some more details, it sounds like you want something like this...

 

$output = preg_replace("#(<img[^>]*>)#i", "<div><a>$1</a></div>", $input);

 

Link to comment
Share on other sites

I used your code like this..

 

<?
$input	= <<<END
some content
<img src="image1.jpg" alt="" width="100" height="150" /> some content. <div><img src="image2.jpg" alt="" width="100" height="150" /></div> some content
<img src="image3.jpg" alt="" width="100" height="150" /> some content..<div><a href="#"><img src="image4.jpg" alt="" width="100" height="150" /></a></div> some content
END;

//preg_match_all("#<img[^>]>#i", $input, $out);
preg_match_all("#<img[^>]*>#i", $input, $out);

var_dump($out);

?>

 

Sorry If am wrong. It finds all the images...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.