joshis Posted November 13, 2009 Share Posted November 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181364-how-to-find-only-inline-image-tags/ Share on other sites More sharing options...
cags Posted November 13, 2009 Share Posted November 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181364-how-to-find-only-inline-image-tags/#findComment-956726 Share on other sites More sharing options...
joshis Posted November 13, 2009 Author Share Posted November 13, 2009 sorry cags, it is not working... Quote Link to comment https://forums.phpfreaks.com/topic/181364-how-to-find-only-inline-image-tags/#findComment-956728 Share on other sites More sharing options...
cags Posted November 13, 2009 Share Posted November 13, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/181364-how-to-find-only-inline-image-tags/#findComment-956731 Share on other sites More sharing options...
joshis Posted November 13, 2009 Author Share Posted November 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181364-how-to-find-only-inline-image-tags/#findComment-956737 Share on other sites More sharing options...
cags Posted November 13, 2009 Share Posted November 13, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/181364-how-to-find-only-inline-image-tags/#findComment-956741 Share on other sites More sharing options...
joshis Posted November 13, 2009 Author Share Posted November 13, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/181364-how-to-find-only-inline-image-tags/#findComment-956754 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.