Quantcast
Channel: Lucee Dev - Latest topics
Viewing all articles
Browse latest Browse all 430

How To: Get Open Graph meta Tag Values

$
0
0

Didnt’ know where else to post this but felt I should share it as its pretty useful and GPT made it work for me. In case you need to get the OG (open graph) tag values from any given URL - I need it to build dynamic content for a newsletter for instance. Hope someone finds it useful.

<!---  The url to test --->
<cfset theurl="http://somewhere.com">
<cfhttp url="#theurl#" result="presult">
<cfset htmlString = '#presult.filecontent#'>
<!--- Initialize a structure to hold the property-content pairs --->
<cfset ogProperties = structNew()>

<!--- Use a regular expression to find all meta tags --->
<cfset metaTags = reMatchNoCase('<meta [^>]*>', htmlString)>

<!--- Loop through each tag to check for og: properties and extract content value --->
<cfloop array="#metaTags#" index="tag">
    <!--- Check if the property attribute contains 'og:' --->
    <cfif reFindNoCase('property="og:', tag)>
        <!--- Extract the property name, ensuring the pattern does not include the closing quote --->
        <cfset propertyName = reReplaceNoCase(tag, '.*?property="([^"]*)".*', '\1')>
        <!--- Extract the content value from the tag, ensuring the pattern does not include the closing quote --->
        <cfset contentValue = reReplaceNoCase(tag, '.*?content="([^"]*)".*', '\1')>
        <!--- Assign the content value to the property name in the structure --->
        <cfset ogProperties[propertyName] = contentValue>
    </cfif>
</cfloop>

<!--- Display the extracted og properties and their content values --->
<!--- <cfdump var="#ogProperties#"> --->
<cfoutput>
<h3 style="background:gray;font-family:Arial;">Open Graph Tags from: #theurl#</h3>
<font style="font-family:Arial;">
  <cfloop collection="#ogProperties#" item="key">
<b>#key# - </b> #ogProperties[key]# <hr>
     
        </cfloop>
</font>
</cfoutput>

5 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 430

Trending Articles