?
Solved

Converting a URL within a string to an active URL?

Posted on 2010-09-23
12
Medium Priority
?
315 Views
Last Modified: 2012-05-10
Hi Folks,

I'm working on some output at the moment that returns plain text, but within that there may be a url (without HTML tags though).

I'd like to convert these into 'proper' urls, so they are clickable at the point of output.

I've found something from another piece of code (TwitterCOM) that appears to do this, however I don't seem to be able to find a way of referencing it.  CFScript is something I've never really used before.

My output is relatively simple, with the string refenced by #i.text#, so I assume I need to use a CFSet to convert, then output the cfset result instead.

I've attached the cfscript I found, though obviously it was written for a different project so I'm not sure if theres something in there specific to the other bit that is stopping it from working for me.
<cfscript>
function activateURL(sActive) {
    var nextMatch = 1;
    var objMatch = "";
    var outstring = "";
    var thisURL = "";
    var thisLink = "";
    
    do {
        objMatch = REFindNoCase("(((https?:|ftp:|gopher:)\/\/)|(www\.|ftp\.))[-[:alnum:]\?%,\.\/&##!;@:=\+~_]+[A-Za-z0-9\/]", sActive, nextMatch, true);
        if (objMatch.pos[1] GT nextMatch OR objMatch.pos[1] EQ nextMatch) {
            outString = outString & Mid(String, nextMatch, objMatch.pos[1] - nextMatch);
        } else {
            outString = outString & Mid(String, nextMatch, Len(sActive));
        }
        nextMatch = objMatch.pos[1] + objMatch.len[1];
        if (ArrayLen(objMatch.pos) GT 1) {
            // If the preceding character is an @, assume this is an e-mail address
            // (for addresses like admin@ftp.cdrom.com)
            if (Compare(Mid(String, Max(objMatch.pos[1] , 1), 1), "@") NEQ 0) {
                thisURL = Mid(String, objMatch.pos[1], objMatch.len[1]);
                thisLink = "<A target=twitter HREF=""";
                switch (LCase(Mid(String, objMatch.pos[2], objMatch.len[2]))) {
                    case "www.": {
                        thisLink = thisLink & "http://";
                        break;
                    }
                    case "ftp.": {
                        thisLink = thisLink & "ftp://";
                        break;
                    }
                }
                thisLink = thisLink & thisURL & """";
                thisLink = thisLink & ">" & thisURL & "</A>";
                outString = outString & thisLink;
                // String = Replace(String, thisURL, thisLink);
                // nextMatch = nextMatch + Len(thisURL);
            } else {
                outString = outString & Mid(String, objMatch.pos[1], objMatch.len[1]);
            }
        }
    } while (nextMatch GT 0);
        
    // Now turn e-mail addresses into mailto: links.
    outString = REReplace(outString, "([[:alnum:]_\.\-]+@([[:alnum:]_\.\-]+\.)+[[:alpha:]]{2,4})", "<A HREF=""mailto:\1"">\1</A>", "ALL");
        
    return outString;
}
</cfscript>

Open in new window

0
Comment
Question by:RedBullLee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 36

Expert Comment

by:SidFishes
ID: 33745808
can you provide a sample of the text you are converting?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33745933
As already mentioned, we'd need to see your code, but to clarify...


>> however I don't seem to be able to find a way of referencing it.  CFScript is something
>> I've never really used before.

That snippet is the same as a CFFUNCTION, only written in cfscript instead of cfml.   So you call it like any other function.  Just pass in the URL you want to "activate"

<cfset result = activateURL("http://www.yoururlhere.com")>
<cfoutput>#result#</cfoutput>


0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 33745963
Check this UDF

http://cflib.org/index.cfm?event=page.udfbyid&udfid=691

it can Solve ur pupose!

just include it using the cfinclude tag and usage is given in the link i have provided, it will wrap with links all the http:// it finds in the string
0
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 

Author Comment

by:RedBullLee
ID: 33746139
Thanks for the prompt replies :-)

The string returned could be as simple as this;

<cfset thestring='Visit http://www.website.com for info'>

And it's returned like this;

<cfoutput>#thestring#</cfoutput>

(The actual string and output is obviously slightly different as it comes from a var loop, but the concept is the same - I've just simplified it).

I've put the cfscript in a seperate cfm file and cfincluded it at the top of the page.

When I use this;

<cfset newstring = activateURL("#thestring#")>

I get the following error;

Variable STRING is undefined.

(again, the actual variables used are different - but none of them are just 'string').

@myselfrandhawa - many thanks for the recommendation, I'll check that out too.  I'd like to try to get the code I found working if possible, if I can understand how to make it work, there are a few other parts within the package that will be useful to me too.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33746156
Umm.. did you read the question ? That is the function he is ALREADY using ...
0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 33746182
yes! i am sorry, mistakenly written again
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33746187
>>. Variable STRING is undefined.

There's a bug in the UDF.  Let's see which one of the experts fixes it first ;-)
0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 33746196
Just write it like this

<cfset newstring = #activateURL("thestring")#>

rather than this

<cfset newstring = activateURL("#thestring#")>
0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 33746203
remove the double quotes please
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 total points
ID: 33746228
Just do a find and replace and change the variable "sActive" to "string"


BTW: I'm not sure why credit wasn't included on TwitterCOM, but the author is:

/**
* This function takes URLs in a text string and turns them into links.
* Version 2 by Lucas Sherwood, lucas@thebitbucket.net.
* Version 3 Updated to allow for ;
*
* @param string      Text to parse. (Required)
* @param target      Optional target for links. Defaults to "". (Optional)
* @param paragraph      Optionally add paragraphFormat to returned string. (Optional)
* @return Returns a string.
* @author Joel Mueller (lucas@thebitbucket.netjmueller@swiftk.com)
* @version 3, August 11, 2004
*/
0
 

Author Comment

by:RedBullLee
ID: 33746296
Thanks _agx_, that did the trick perfectly.  I understand it a bit better now too :-)

I've added the credit to the top of my function file too, thanks for also pointing that out.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33746411
>> I've added the credit to the top of my function file too

   Much appreciated :)
0

Featured Post

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

800 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