Jump to content

preg_match_all to find image src


sanfly

Recommended Posts

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?

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.