开发者

Output text with hyperlinks

开发者 https://www.devze.com 2023-01-18 10:14 出处:网络
If you 开发者_JAVA技巧have text coming from a database such as: \"New Apple TV Offers 8 GB of Internal Storage, 256 MB RAM http://t.co/fQ7rquF\"

If you 开发者_JAVA技巧have text coming from a database such as:

 "New Apple TV Offers 8 GB of Internal Storage, 256 MB RAM http://t.co/fQ7rquF"

Is there a helper method that takes that text and wraps the web address as a anchor tag?


Off the top of my head I would run a regular expression to match a link pattern and throw the anchor tags around it.

Found this on google:

using System.Text.RegularExpressions; 

public string ConvertURLsToHyperlinks(string sInput) 
{ 
    return Regex.Replace(sInput, @"(\bhttp://[^ ]+\b)", @"<a href=""$0"">$0</a>"); 
}

It looks for "http://" followed by anything that's not a space, separated by word boundaries.


i believe there is no such helper method but u can create one

public static class helper{
public static string AnchorHelper(this htmlHelper helper, string text)
{
   //here u can use kevin's function to generate anchors
   return ConvertURLsToHyperlinks(text);  
}
}

you have to add corresponding namespace in ur view and then u can just use it like other html helpers

<%=Html.AnchorHelper(TextwithUrls)%>
0

精彩评论

暂无评论...
验证码 换一张
取 消