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-18-2005 @ 4:07 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
New Member
Posts: 12
Joined: Dec 2005

Hi,

I'm trying to figure out how to remove entire href tags from a block of text. I need to be able to search on a name and then if it's found as the href, remove the tag from around the text inside it.

For example, I have the following test:

This is a test.  This is a <a href="link.html">link</a>.

If I search for link.html and it's found (as it would be in the above) I want to strip out the href tag it's contained in. So I'd want the output to be:

This is a test.  This is a link.

I'm guessing I need to use a regular expression to match something like this? I just have no clue how to formulate something like that.

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

http://www.cflib.org/udf.cfm?ID=767

http://www.rachelqueensg.com

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

That function strips out all hrefs within a block of text tho, correct?  I want to be able to search on a keyword and only strip out those hrefs that use that keyword.

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

Okay, I wrote a function for you so that you can supply a particular link to remove:


<cfscript>
     /**
      * Strips HTML links from the provided string
      *
      * @param strIn          - String to strip links from (REQUIRED)
      * @param linkText     - text of the link to strip (OPTIONAL - if provided, will strip ONLY that particular link)
      * @return Returns the input string minus the stripped links
      * @author Matt Quackenbush (fiberdood@hotmail.com)
      * @version 1, December 18, 2005
      */
     function stripLinks(strIn) {
          variables.rtn = arguments.strIn;
          if (arrayLen(arguments) GT 1) {
               arguments.linkText = arguments[2];
          }
          if (NOT isDefined("arguments.linkText")) {
               variables.rtn = reReplaceNoCase(variables.rtn, "<[[:space:]]*a[[:space:]]+[^>]*>", "", "all");
               variables.rtn = reReplaceNoCase(variables.rtn, "<[[:space:]]*/a[[:space:]]*>", "", "all");
          } else {
               variables.blStart = reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*>(?=[[:space:]]*#arguments.linkText#[\.\?\! ]*)", variables.rtn);
               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.linkText, "all");
          }
          return variables.rtn;
     }
</cfscript>


Enjoy Smile


http://www.rachelqueensg.com

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

Wow dude.. thank you very much!  Didn't expect someone to write a custom function for me.

It's much appreciated tho.  I might have some questions as to how it works, as I want to understand how it actually strips the href tags out.  Smile

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

No problem.  Hopefully it'll do the trick for you.  To use it, simply feed in the string that you want link(s) stripped from.  Then, if you want to search for specific link text (i.e. the text between the opening and closing anchor tags '<a href="">LINK TEXT</a>'), feed that in as the 2nd parameter.

<cfscript>
           variables.str = "<p>This is a <a href=""#cgi.script_name#"">test link</a>.  And this is <a href=""#cgi.script_name#"">another one</a>.</p>";
     variables.str2 = stripLinks(variables.str);
     variables.str3 = stripLinks(variables.str, "test link");
</cfscript>


The function can be modified to look for a particular URL instead of the link text, if need be.

http://www.rachelqueensg.com

This message was edited by mquack on 12-18-05 @ 10:38 PM

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

Hi,

I've been trying to use the function you wrote, but am having some problems.  Instead of searching for the link text inside the <a href=""></a> tags, I need to search on the text that's contained within the href="" (between the quotes.

How would I modify the function to do that?  I'm assuming it has something to do with this line?

variables.blStart = reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*>(?=[[:space:]]*#arguments.linkText#[\.\?\! ]*)", variables.rtn);



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

Sorry, misunderstood (again) what you were wanting.  Try this instead...

<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)
      * @return Returns the input string minus the stripped links
      * @author Matt Quackenbush (fiberdood@hotmail.com)
      * @version 1.2, December 19, 2005
      */
     function stripLinks(strIn) {
          variables.rtn = arguments.strIn;
          if (arrayLen(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 {
               variables.blStart = reFindNoCase("<[[:space:]]*a[[:space:]]+[^>]*(?=[[:space:]]*#arguments.href#)[[:space:]]*[^>]*>", variables.rtn);
               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, "all");
          }
          return variables.rtn;
     }
</cfscript>

This function will do work two ways now:

1) It will strip ALL links from the provided text, OR
2) It will strip ONLY links that match the string you provide in argument #2, and replace that link with the string that you provide in argument #3.

Note that if you provide argument #2, you MUST provide argument #3 as well, or the function will still strip ALL links from the provided string.

Ex.

<cfscript>
     variables.str = "<p>This is a <a href=""testlink.htm"">test link</a>.  And this is <a href=""#cgi.script_name#"">another one</a>.</p>";
     variables.str2 = stripLinks(variables.str);
     variables.str3 = stripLinks(variables.str, "testlink.htm", "test link");
     writeOutput("original string: #variables.str#<br />string minus all links: #variables.str2#<br />string minus only the 'testlink.htm' link: #variables.str3#");
</cfscript>


Let me know if that's what you need.

http://www.rachelqueensg.com

This message was edited by mquack on 12-19-05 @ 1:54 PM

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

That's getting closer..  but I'm not going to be able to tell what the third argument is beforehand, if you follow my logic there.  IE:  I could have:

This is some <a href="link.html">crazy text</a>.

I can pass the text and the search string of "link.html", but I'll have no way of knowing what text would actually be contained within the link tags that are being stripped.

I'd like to just be able to pass the text to search on, and the search string, and have the link tags be removed, leaving whatever text was contained within them.

Again, thanks for all your help on this.  It's really helping me out and much appreciated.

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

Okay, it can be modified to do that as well, but let me ask this:

Let's say that someone puts a link to billyray.com and that is what you want stripped out.  But, let's say that they input the link as <a href="http://billyray.com">billyray.com</a>.  If you strip out the link, but leave 'billyray.com', doesn't that defeat the purpose?  Or no?  Just asking Smile

I'll get a mod for you here in a few.

http://www.rachelqueensg.com

PAGE: 1 2

Website Designed and Developed by Pablo Varando.