diff --git a/MDHTMLLabel/MDHTMLLabel.m b/MDHTMLLabel/MDHTMLLabel.m
index 5fe3396..4f45c02 100644
--- a/MDHTMLLabel/MDHTMLLabel.m
+++ b/MDHTMLLabel/MDHTMLLabel.m
@@ -350,7 +350,10 @@ - (NSString *)detectURLsInText:(NSString *)text
{
if (match.resultType == NSTextCheckingTypeLink)
{
- if (match.range.location > 6 && ![[text substringWithRange:NSMakeRange(match.range.location-6, 4)] isEqualToString:@"href"])
+ // if the URL location is greater than 6 (enough space for an anchor tag) and there's a 'href' before it, or if there's a closing anchor tag
+ // after it, then we dont wrap the URL in anchor tags
+ if (!(match.range.location > 6 && [[text substringWithRange:NSMakeRange(match.range.location-6, 4)] isEqualToString:@"href"])
+ && (![[text substringWithRange:match.range] rangeOfString:@"a>"].length > 0))
{
text = [text stringByReplacingCharactersInRange:match.range
withString:[NSString stringWithFormat:@"%@", match.URL.absoluteString, match.URL.absoluteString]];