Skip to content

Instantly share code, notes, and snippets.

@dieseltravis
Created January 9, 2009 16:44
Show Gist options
  • Select an option

  • Save dieseltravis/45165 to your computer and use it in GitHub Desktop.

Select an option

Save dieseltravis/45165 to your computer and use it in GitHub Desktop.
localize all href/src for a xml/html content string
Public Shared Function LocalizeUrls(ByVal originalUrl As String, ByVal content As String) As String
' make image src's and anchor href's absolute in the string _sourceHtml
' get path for source
Dim siteUri As New Uri(originalUrl)
' modify img[@src] and a[@href]
Dim sources As MatchCollection = Regex.Matches(content, "(src|href)=([""'])(?!\#|http)([^\2]+?)(\2)", RegexOptions.Compiled Or RegexOptions.CultureInvariant Or RegexOptions.IgnoreCase)
For Each src As Match In sources
Dim attribute As String = src.Groups(1).Value
Dim quote As String = src.Groups(2).Value
Dim url As String = src.Groups(3).Value
' this will handle links like this: "../page", "/page", "page"
Dim newUri As New Uri(siteUri, url)
Dim originalSrc As String = src.Value
Dim newSrc As String = attribute & "=" & quote & newUri.AbsoluteUri() & quote
content = content.Replace(originalSrc, newSrc)
Next
Return content
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment