List All Images Used In Articles From A Content Management System

Posted on 2007-07-22
Last Modified: 2013-12-24
Can someone help me with what I think is probably a simple solution?  I simply want to create a list of all images used in articles from a content management system.  So I need to start with

<cfquery name="GetArticles" datasource="myDB">
Select * from myArticles

Loop through them...

And produce a list like


I know I need a regular expression but can't seem to come up with the logic to put it all togther.

500 points for the solution
Question by:SiriusPhil
    LVL 39

    Expert Comment


    How about this?    you have to exchagne the imageFile in your query with the real column name..

    <cfoutput query="getArticles">

    >  I know I need a regular expression but can't seem to come up with the logic to put it all togther.

    I think you've left out the details of your question that may lead someone to think of a regular expression.   We don't know what data you have in your table and if  "/images/image1.jpg" is in a column of your table, you just need to display it as I've shown.  

    If there is something else you're looking for, please update with more information about what it is you have and what you want..


    Author Comment

    There can be many images in each database entry.  The image tags are stored in the database field along with a ton of other HTML.
    LVL 39

    Expert Comment

    Something like this...   (not tested)

     This should find the image tags (<img>)

     <cfset locations =  refind("<img[^>]*>", theHTML,  1,  TRUE)>

     refind (when using TRUE at the end) will return an array of positions and lengths..

     <cfloop index="i" from="1" to="#ArrayLen(locations.pos)#">
       #Mid(theHTML,  locations.pos[i],  locations.len[i])# <br>
    LVL 14

    Accepted Solution

    Here is a function that will return an array of the values within the src attributes of all img tags in your string. Currently it requires that all the src attributes be enclosed in quotes ("), but it can be modified to look for single quotes as wellif needed.

    <cffunction name="FindIMGs" output="true" returntype="array">

       <cfargument name="text" type="string" required="yes">
       <!--- Define local variables --->
       <cfset var results=ArrayNew(1)>
       <cfset var pos=1>
       <cfset var subex="">
       <cfset var done=false>

       <cfloop condition="not done">

          <!--- Perform search --->
          <cfset subex=reFind("<img[^>]*>", arguments.text, pos, true)>
          <!--- Anything matched? --->
          <cfif subex.len[1] is 0>
             <cfset done=true>
                  <!--- find src attribute and get its value --->
                   <cfset srcA=find("src",mid(text,subex.pos[1],subex.len[1]))>
                 <cfset srcB=find("""",mid(text,subex.pos[1],subex.len[1]),srcA)>
                 <cfset srcC=find("""",mid(text,subex.pos[1],subex.len[1]),srcB+1)>
                 <cfset srcValue=mid(mid(text,subex.pos[1],subex.len[1]),srcB+1,srcC-srcB-1)>
             <!--- Got one, add to array if it's not ther already --->
                 <cfif not listfind(arraytolist(results),srcValue)>
                 <cfset arrayappend(results,srcValue)>
             <!--- Reposition start point --->
             <cfset pos=subex.pos[1]+subex.len[1]>

       <!--- and return results --->
       <cfreturn results>

    <cfset IMGArray = FindIMGs(MyArticleContent)>

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    json_decode return null? 8 46
    ColdFusion - Import large XLS files 4 52
    Running Guacamole on port 80 5 72
    connection string question 2 61
    The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
    Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now