|
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Regex wrapping div tags round img tags
Hi,Matthew Allen 07/02/09 06:01 A A regex question on this list that doesn't already have 5 answers? Wow.Nathan Strutz 07/02/09 12:36 P > Holiday weekend must be in full effect already.Robert Harrison 07/02/09 12:42 P >A regex question on this list that doesn't already have 5 answers? Wow.Peter Boughton 07/02/09 05:17 P Regular Expressions are not designed for HTML manipulation of this sort.Peter Boughton 07/02/09 05:13 P Hi, I'll really appreciate if anyone could help. I have image tags outputted to my page from a database, what can I write to to wrap <div> tags round the <img> tags so e.g; <img src="...images/jpgs/ducks.jpg" alt="" /> I would like to change it to; <div class="cover"><img src="...images/jpgs/ducks.jpg" alt="" /></div> Many thanks, Matt A regex question on this list that doesn't already have 5 answers? Wow. Holiday weekend must be in full effect already. Ok, you want something like this... reReplace(string, "(<img[^>]+>)", "<div class='cover'>\1</div>", "ALL") That is... ( -- capture a sub-group <img -- html image tags [^>] -- anything not a tag closer, > + -- for at least one character > -- followed by the tag closer ) -- end the subgroup and the replacement: <div class='cover'>\1</div> -- \1 is a backreference to the first (and only) sub-group we captured nathan strutz [Blog and Family @ http://www.dopefly.com/] [AZCFUG Manager @ http://www.azcfug.org/] [Twitter @nathanstrutz] On Thu, Jul 2, 2009 at 2:56 AM, Matthew Allen <a.matthew14@yahoo.com> wrote: ----- Excess quoted text cut - see Original Post for more ----- > Holiday weekend must be in full effect already. Yeah, my brain is just outside the door waiting for the rest of me to join it. Robert B. Harrison Director of Interactive Services Austin & Williams 125 Kennedy Drive, Suite 100 Hauppauge NY 11788 P : 631.231.6600 Ext. 119 F : 631.434.7022 http://www.austin-williams.com Great advertising can't be either/or. It must be &. Plug in to our blog: A&W Unplugged http://www.austin-williams.com/unplugged __________ Information from ESET Smart Security, version of virus signature database 4209 (20090702) __________ The message was checked by ESET Smart Security. http://www.eset.com >A regex question on this list that doesn't already have 5 answers? Wow. >Holiday weekend must be in full effect already. Nah, HoF was broken (again) earlier. I tried posting half a dozen times throughout the day but kept getting errors. Anyway, a single but significant difference in our answers: you don't need a capturing group for the whole regex. The \0 backreference is always available, and contains the entire matched text. Regular Expressions are not designed for HTML manipulation of this sort. You should really be using a DOM parser and manipulating the data as a set of nodes. That said, here is a very basic regex replace that will work in the majority (but not all) situations: rereplace( SomeHtml , '<img[^>]+>' , '<div class="cover">\0</div>' , 'all' )
|
Mailing Lists
|
Latest Fusion Authority Articles
|
||||||