> Hi,
> I need to be able to take in a specific body of text,
> search the text for any html links in it, and ensure that
> the html links have a target="_blank" attribute so they
> will all open in a new window, and then output the body of
> text with the newly modified links (or left alone if they
> were okay).
> I know that I will probably need to do this with
> REReplaceNoCase, but I'm not an expert in regular
> expressions by any means. I started by trying to find the
> links with this:
> REFindNoCase("<a[.]*href[.]*>",textbody)
> But that's not working right. If anyone working today is
> familiar with regexps, I could use some help.
If you require the input to be valid xml, then you can also use an XSL
transformation:
<cfset text = REReplaceNoCase(text,'target="[^"]*"',"","ALL")>
<cfset text = ReplaceNoCase(text,"<a ",'<a target="_blank"',"ALL")>
Or
<cfsavecontent variable="xsl">
<xsl:stylesheet>
<xsl:output type="xml" />
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="*[translate(local-name(),'A','a')='a']">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
</cfsavecontent>
<cfset text = XMLTransform(XMLParse(text),xsl)>
It's more code yes, but if you're requiring XML content, this will be
more bullet-proof than the regular expression solution. On CF7 you can
put the XSL sheet in a separate template and just use the absolute
path to the file in the XMLTransform function, i.e.:
<cfset text =
XMLTransform(XMLParse(text),expandPath("target_blank.xsl"))>
s. isaac dealey 954.522.6080
new epoch : isn't it time for a change?
add features without fixtures with
the onTap open source framework
http://www.fusiontap.com
http://coldfusion.sys-con.com/author/4806Dealey.htm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Logware (www.logware.us): a new and convenient web-based time tracking
application. Start tracking and documenting hours spent on a project or with a
client with Logware today. Try it for free with a 15 day trial account.
http://www.houseoffusion.com/banners/view.cfm?bannerid=67
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:217296
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54