EasyCFM.COM ColdFusion Forums / Coding Help! / Strip hrefs from block of text

   Reply to Discussion | New Discussion << previous || next >> 
Posted By Discussion Topic: Strip hrefs from block of text -- page: 1 2

book mark this topic Printer-friendly Version  send this discussion to a friend  new posts last

dante2010xxx
12-19-2005 @ 2:53 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
New Member
Posts: 12
Joined: Dec 2005

I know it seems kinda strange, but that is what I need it to do.

It's for a CMS system with a link manager system.  If a page is deleted from the CMS I need to go thru the content of all other pages and strip any links that contained that deleted page.  I don't want to strip the entire link tho, as that might cause sentences to be fragmented.

IE:  

This is a <a href="link.cfm">crazy link</a> that I think is cool.

If I remove "<a href="link.cfm">crazy link</a> then I end up with:

This is a that I think is cool.

So I'd rather just leave the text that was there in and strip off the link.



mquack
12-19-2005 @ 2:57 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1544
Joined: Jan 2005

Okie dokie smokie...

<cfscript>
     /**
      * Strips HTML links from the provided string
      *
      * @param strIn               - String to strip links from (REQUIRED)
      * @param href               - href of the link to strip (OPTIONAL - if provided, will strip ONLY that particular link [each occurrence])
      * @param replaceText     - text to put in place of the stripped link (OPTIONAL - if provided along with an 'href' argument, will replace the link with the provided text)
      * @return Returns the input string minus the stripped links
      * @author Matt Quackenbush (fiberdood@hotmail.com)
      * @version 1.3, December 19, 2005
      */
     function stripLinks(strIn) {
          variables.rtn = arguments.strIn;
          if (arrayLen(arguments) GTE 2) {
               arguments.href = arguments[2];
               if (arrayLen(arguments) GT 2) {
                    arguments.replaceText = arguments[3];
               }
          }
          if (NOT isDefined("arguments.href")) {
               variables.rtn = reReplaceNoCase(variables.rtn, "<[[:space:]]*a[[:space:]]+[^>]*>", "", "all");
               variables.rtn = reReplaceNoCase(variables.rtn, "<[[:space:]]*/a[[:space:]]*>", "", "all");
          } else if (isDefined("arguments.href") AND NOT isDefined("arguments.replaceText")) {
               while (reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", variables.rtn) NEQ 0) {
                    variables.blStart = reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", variables.rtn);
                    variables.blStop = reFindNoCase("<[[:space:]]*/a[[:space:]]*>", variables.rtn, variables.blStart+1);
                    variables.blStop2 = reFindNoCase(">", variables.rtn, variables.blStop)+1;
                    variables.rtn = removeChars(variables.rtn, variables.blStop, variables.blStop2-variables.blStop);
                    variables.rtn = reReplaceNoCase(variables.rtn, "<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", "");
               }
          } else {
               while (reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", variables.rtn) NEQ 0) {
                    variables.blStart = reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", variables.rtn);
                    variables.rtn = reReplaceNoCase(variables.rtn, "<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", "");
                    variables.blStop = reFindNoCase(">", variables.rtn, variables.blStart+1);
                    variables.blStop = reFindNoCase("<[[:space:]]*/a[[:space:]]*>", variables.rtn, variables.blStart+1);
                    variables.blStop = reFindNoCase(">", variables.rtn, variables.blStop)+1;
                    variables.bl = mid(variables.rtn, variables.blStart, variables.blStop-variables.blStart);
                    variables.rtn = reReplaceNoCase(variables.rtn, variables.bl, arguments.replaceText);
               }
          }
          return variables.rtn;
     }
     variables.str = "<p>This is a <a href=""testlink.htm"">test link</a>.  
And this is <a href=""#cgi.script_name#"">another one</a>.  
And this is a link to <a href=""http://www.myspace.com/billy/was/here/index.htm"">myspace.com</a>.  
And this is another link to <a href=""http://www.myspace.com/billy/was/here/index.htm"">myspace.com</a        >.</p>";
     variables.str2 = stripLinks(variables.str);
     variables.str3 = stripLinks(variables.str, "testlink.htm");
     variables.str4 = stripLinks(variables.str, "myspace.com", "[forbidden link]");
     writeOutput("original string: #variables.str#<br />string minus all links: #variables.str2#<br />string minus only the 'testlink.htm' link: #variables.str3#<br />string replacing 'myspace.com' links: #variables.str4#<br />");
</cfscript>

Or, if you're like me and prefer your functions to be placed in a CFC...

<!--- BEGIN stripLinks() --->
<cffunction name="stripLinks" hint="Removes links (anchor tags) from the provided string; returns the input string minus the stripped links" returntype="string" output="false">
     <!--- set the method arguments --->
     <cfargument name="strIn" hint="REQUIRED INPUT! Provides the string to be manipulated" required="yes" type="string" />
     <cfargument name="href" hint="Provides the href to search for when stripping links; if provided, will ONLY STRIPS MATCHING LINKS! (e.g. providing 'textlink.htm' to this argument will result in links being stripped ONLY if they're formed as '<a href="TEXTLINK.HTM">bla bla</a>')" required="no" type="string" />
     <cfargument name="replaceText" hint="If provided in conjunction with the 'href' argument, the stripped link will be replaced with this text" required="no" type="string" />
     
     <cfscript>
          variables.rtn = arguments.strIn;
          if (NOT isDefined("arguments.href")) {
               variables.rtn = reReplaceNoCase(variables.rtn, "<[[:space:]]*a[[:space:]]+[^>]*>", "", "all");
               variables.rtn = reReplaceNoCase(variables.rtn, "<[[:space:]]*/a[[:space:]]*>", "", "all");
          } else if (isDefined("arguments.href") AND NOT isDefined("arguments.replaceText")) {
               while (reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", variables.rtn) NEQ 0) {
                    variables.blStart = reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", variables.rtn);
                    variables.blStop = reFindNoCase("<[[:space:]]*/a[[:space:]]*>", variables.rtn, variables.blStart+1);
                    variables.blStop2 = reFindNoCase(">", variables.rtn, variables.blStop)+1;
                    variables.rtn = removeChars(variables.rtn, variables.blStop, variables.blStop2-variables.blStop);
                    variables.rtn = reReplaceNoCase(variables.rtn, "<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", "");
               }
          } else {
               while (reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", variables.rtn) NEQ 0) {
                    variables.blStart = reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", variables.rtn);
                    variables.rtn = reReplaceNoCase(variables.rtn, "<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", "");
                    variables.blStop = reFindNoCase(">", variables.rtn, variables.blStart+1);
                    variables.blStop = reFindNoCase("<[[:space:]]*/a[[:space:]]*>", variables.rtn, variables.blStart+1);
                    variables.blStop = reFindNoCase(">", variables.rtn, variables.blStop)+1;
                    variables.bl = mid(variables.rtn, variables.blStart, variables.blStop-variables.blStart);
                    variables.rtn = reReplaceNoCase(variables.rtn, variables.bl, arguments.replaceText);
               }
          }
          return variables.rtn;
     </cfscript>
</cffunction>
<!--- END stripLinks() --->

Now it works 3 ways:

1) Strip all links in the provided string (provide ONLY the input string)

2) Strip only matching links, leaving the original "link text" in place (provide the string AND the href argument)

3) Strip only matching links, and replace them with your desired text (provide the string, the href argument, and the replaceText argument)

http://www.rachelqueensg.com

This message was edited by mquack on 12-19-05 @ 3:04 PM

dante2010xxx
12-19-2005 @ 3:32 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
New Member
Posts: 12
Joined: Dec 2005

Just tried it out.. and it seems to still be replacing the text contained in the link with whatever I pass as the second (href) arg.

So if I have:

<cfset content="This is a <a href="link.cfm">crazy link</a> that I think is cool."

And I call the function with:

<cfset newContent = stripLinks(content, 'link.cfm')>

I end up with:

This is a link.cfm that I think is cool.

... where I need it to be:

This is a crazy link that I think is cool.

mquack
12-20-2005 @ 2:15 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1544
Joined: Jan 2005

Hmmmm... for some reason I did not get an email notification of your message.  Sorry about the delay in responding... I thought you hadn't responded...

Anyways, for some reason the <cfscript> version wasn't working properly (although the <cffunction> tag version does) with your example, even though they both worked fine with my example.  Odd indeed.  Anyways, try this version...

<cfscript>
     /**
      * Strips HTML links from the provided string
      *
      * @param strIn               - String to strip links from (REQUIRED)
      * @param href               - href of the link to strip (OPTIONAL - if provided, will strip ONLY that particular link [each occurrence])
      * @param replaceText     - text to put in place of the stripped link (OPTIONAL - if provided along with an 'href' argument, will replace the link with the provided text)
      * @return Returns the input string minus the stripped links
      * @author Matt Quackenbush (fiberdood@hotmail.com)
      * @version 1.5, December 20, 2005
      */
     function stripLinks(strIn) {
          variables.rtn = arguments.strIn;
          if (structCount(arguments) EQ 2) {
               arguments.href = arguments[2];
          } else if (structCount(arguments) EQ 3) {
               arguments.href = arguments[2];
               arguments.replaceText = arguments[3];
          }
          if (NOT isDefined("arguments.href")) {
               variables.rtn = reReplaceNoCase(variables.rtn, "<[[:space:]]*a[[:space:]]+[^>]*>", "", "all");
               variables.rtn = reReplaceNoCase(variables.rtn, "<[[:space:]]*/a[[:space:]]*>", "", "all");
          } else if (isDefined("arguments.href") AND NOT isDefined("arguments.replaceText")) {
               while (reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", variables.rtn) NEQ 0) {
                    variables.blStart = reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", variables.rtn);
                    variables.blStop = reFindNoCase("<[[:space:]]*/a[[:space:]]*>", variables.rtn, variables.blStart+1);
                    variables.blStop2 = reFindNoCase(">", variables.rtn, variables.blStop)+1;
                    variables.rtn = removeChars(variables.rtn, variables.blStop, variables.blStop2-variables.blStop);
                    variables.rtn = reReplaceNoCase(variables.rtn, "<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", "");
               }
          } else {
               while (reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", variables.rtn) NEQ 0) {
                    variables.blStart = reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", variables.rtn);
                    variables.rtn = reReplaceNoCase(variables.rtn, "<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", "");
                    variables.blStop = reFindNoCase(">", variables.rtn, variables.blStart+1);
                    variables.blStop = reFindNoCase("<[[:space:]]*/a[[:space:]]*>", variables.rtn, variables.blStart+1);
                    variables.blStop = reFindNoCase(">", variables.rtn, variables.blStop)+1;
                    variables.bl = mid(variables.rtn, variables.blStart, variables.blStop-variables.blStart);
                    variables.rtn = reReplaceNoCase(variables.rtn, variables.bl, arguments.replaceText);
               }
          }
          return variables.rtn;
     }
</cfscript>



http://www.rachelqueensg.com

This message was edited by mquack on 12-20-05 @ 2:21 PM

dante2010xxx
12-20-2005 @ 3:31 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
New Member
Posts: 12
Joined: Dec 2005

That works like a charm.

Thanks again for all your help.  Even tho you did all the work, I did learn something about how to go about solving a problem like this.  Smile



mquack
12-20-2005 @ 4:19 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1544
Joined: Jan 2005

You're quite welcome, glad to have helped!

http://www.rachelqueensg.com

PAGE: 1 2

Website Designed and Developed by Pablo Varando.