I had this old snippet of VBScript in my library since 2005.  It basically reads a stream of text, parses it on spaces into an array of “words”, looks for those which begin with “http” and folds that “word” into a hyperlink wrapper.  If that sounds like a crackhead on meth explaining the JFK conspiracy to you on the subway at 1:00 AM, you’re absolutely normal.  That’s okay, let’s move on…

Example text stream

"For a complete list of our crappy products, please visit http://fubar.tarfu.snafu.nothing/crap.html.  Thank you for your time!"

VBScript function

Function Get_Mutilated_Text (textstream)
  Dim word, result : result = ""
  For each word in Split(textstream)
    If Left(Lcase(word),4) = "http" Then
      If Asc(Right(word,1)) < 48 Or Asc(Right(word,1)) > 122 Then
        word = "<a href=""" & Left(word, Len(word)-1) & """>" & Left(word, Len(word)-1) & "</a>" & Right(word,1)
        word = "<a href=""" & word & """>" & word & "</a>"
      End If
    End If
    If result <> "" Then
      result = result & " " & word
      result = word
    End If
  Get_Mutilated_Text = result
End Function

PowerShell function

function Get-Mutilated-Text {
  param (
    [parameter(Mandatory=$True)] [string] $TextStream
  $result = ""
  foreach ($word in $TextStream.Split(' ') ) {
    $lword = $word.ToLower()
    if ($lword.StartsWith("http")) {
      $lastchar = $lword.Substring($lword.Length-1,1)
      $a = [byte][char]$lastchar
      if ($a -lt 48 -or $a -gt 122) {
        $w = $lword.Substring(0,$lword.Length-1)
        $word = "<a href=""$w"">$w</a>$lastchar"
      else {
        $word = "<a href=""$word"">$word</a>"
    $result += " $word"
  return $result.TrimStart()


The reason I didn’t use .EndsWith() for testing the end string character, is that I need to test if the ending character of each word is not alphanumeric (punctuation, etc.).  That way the wrapped hyperlink will cleanly exclude that character from the closing </a> tag, and makes the string more grammatically correcter. 🙂

I’m sure someone else can blow this away with a one-line PowerShell statement, with both hands tied behind their back and both eyes covered with duct tape, in a pitch-black room, heavily sedated, and without a computer.  But this mess works pretty well actually.



Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s