sanfly Posted July 8, 2011 Share Posted July 8, 2011 Hi Im using preg_match_all to find all instances of an image in a string <?php $textareainfo = '<p><img style="float: left;" _mce_style="float: left;" src="http://localhost/dsc-upgrade/img/uploads/content/hist-HappyValley1975.jpg" _mce_src="http://localhost/dsc-upgrade/img/uploads/content/hist-HappyValley1975.jpg" height="177" width="300">Progress with the High noon is going really well and to schedule.</p> <p>All the new high noon towers have been flown in and the old tower sections have been flown out. All the cross heads are on the towers and we have only two more wheel assemblies to fly in. We are currently tightening bolts on the towers, there is close to 150 bolts per tower and these all need to be torqued down to specification and the towers are being wired into the safety and control circuits.</p> <p>Our next milestone is to get the haul rope cable onto the towers and running so we can speed up access. Staff currently have to walk up each day, this will also allow us to better manage the lift through early ice storms. The top of the high flyer has been dismantled. Work is progressing well and we are scheduled to have the lift commissioned and certified by the end of May.</p>'; preg_match_all('/src=([\'"])?(.*?)\\1/', $textareainfo, $output); print_r($output); ?> My problem is that this code is taken from a TinyMCE textarea, and in the image tag it also has _mce_src="http://localhost/dsc-upgrade/img/uploads/content/hist-HappyValley1975.jpg" as well as the standard src So, what I want to do is modify the regex so that it doesnt return _src, only src. Unfortunately Im pretty useless with regex - Ive had a play around but have been unable to figure it out yet. Any help? Quote Link to comment https://forums.phpfreaks.com/topic/241447-preg_match_all-to-find-image-src/ Share on other sites More sharing options...
wildteen88 Posted July 8, 2011 Share Posted July 8, 2011 Use a negative look behind /(?<!_)src=([\'"])?(.*?)\\1/ Quote Link to comment https://forums.phpfreaks.com/topic/241447-preg_match_all-to-find-image-src/#findComment-1240307 Share on other sites More sharing options...
requinix Posted July 8, 2011 Share Posted July 8, 2011 Since TinyMCE will (likely) create valid HTML, you might be better off with something like DOMDocument. $string = " ..."; $dom = new DOMDocument(); $dom->loadHTML($string); $images = $dom->getElementsByTagName("IMG"); foreach ($images as $image) { $src = $image->attributes->getNamedItem("src"); echo "Image: ", ($src ? $src->nodeValue : "(no src)"), " \n"; } Quote Link to comment https://forums.phpfreaks.com/topic/241447-preg_match_all-to-find-image-src/#findComment-1240308 Share on other sites More sharing options...
.josh Posted July 8, 2011 Share Posted July 8, 2011 preg_match_all('~<img[^>]*(?<!_mce_)src\s?=\s?([\'"])((??!\1).)*)[^>]*>~i', $textareainfo, $output); Quote Link to comment https://forums.phpfreaks.com/topic/241447-preg_match_all-to-find-image-src/#findComment-1240310 Share on other sites More sharing options...
sanfly Posted July 8, 2011 Author Share Posted July 8, 2011 Thanks Wildteen88, that worked great Crayon Violet - I did have more complicated series of string searches etc that looked for <img then src (more or less what your regex does from what I can tell) - the only problem was if I added any styles to the image in TinyMCE, it would put the style tag attribute ahead of the src attribute and screw up my script. Thats when I decided I needed to just search for the src attribute. Cheers for your suggestion though. Quote Link to comment https://forums.phpfreaks.com/topic/241447-preg_match_all-to-find-image-src/#findComment-1240313 Share on other sites More sharing options...
.josh Posted July 8, 2011 Share Posted July 8, 2011 Thanks Wildteen88, that worked great Crayon Violet - I did have more complicated series of string searches etc that looked for <img then src (more or less what your regex does from what I can tell) - the only problem was if I added any styles to the image in TinyMCE, it would put the style tag attribute ahead of the src attribute and screw up my script. Thats when I decided I needed to just search for the src attribute. Cheers for your suggestion though. Well my regex will work if you put some other attribute in there. It matches the rest of what's in the img tag by just looking for anything that is not the closing > for the tag. Quote Link to comment https://forums.phpfreaks.com/topic/241447-preg_match_all-to-find-image-src/#findComment-1240315 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.