As a programmer, after completing the design, you must continue to improve the program based on the program's situation and user feedback, so that you can continuously improve your work. After I finished creating the forum for the Software Business Network http://www.bizsofts.com , I found that people always like to add various useful URL links or email addresses to their posts. I did not take this into consideration when designing, so these URL links or email addresses can only be displayed in the form of text instead of hyperlinks. Other people who browse the post must also copy these URL links to the browser. Or copy the email address to Outlook to go to the corresponding link address or send an email to the corresponding email address.
After discovering this problem, I set out to solve it. The first thing is to look up the current code in this area from the Internet. Unfortunately, no articles in this area were found after repeated searches on search engines. Then I thought, I might as well write one myself using ASP.NET.
The key to automatically displaying hyperlinks is how to correctly identify hyperlinks. There is no doubt that the most effective method is to use regular expressions. Regular expressions are text patterns composed of ordinary characters (such as characters a to z) and special characters (called metacharacters). They describe a string matching pattern and can be used to check whether a string contains a certain substring. Replace matching substrings or extract substrings that meet certain conditions from a certain string, etc. The .NET basic class library contains a namespace and a series of classes that can give full play to the power of regular expressions. You can use it to automatically detect URL links or email addresses in text. Let me explain in detail how to use ASP.NET (C#) to achieve our goal step by step:
First, if you want to use regular expressions in ASP.NET (C#), you must include the System.Text.RegularExpressions namespace:
using System.Text.RegularExpressions;
The second step is to use regular expressions to identify URL hyperlinks:
Regex urlregex = new Regex(@"(http://([w.]+/?)S*) ",
RegexOptions.IgnoreCase|RegexOptions.Compiled);
The code here uses regular expressions to identify email addresses:
Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+@[a-zA-Z_0 -9.-]+.w+)",
RegexOptions.IgnoreCase|RegexOptions.Compiled);
The third step, when the program has identified the URL hyperlink or email address, these hyperlinks must be replaced with <a href=...>Hyperlink</a>, so Only then can these texts be displayed as links. I include them all in the function here:
private void Button1_Click(object sender, System.EventArgs e)
{
string strContent = InputTextBox.Text;
Regex urlregex = new Regex(@"(http://([w.]+/?)S*)",
RegexOptions.IgnoreCase| RegexOptions.Compiled);
strContent = urlregex.Replace(strContent,
"<a href="" target="_blank"></a>");
Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+@[a-zA-Z_0-9.-]+.w+)",
RegexOptions.IgnoreCase| RegexOptions.Compiled);
strContent = emailregex.Replace(strContent, "<a href=mailto:></a>");
lbContent.Text += "<br>"+strContent;
}
Through the above steps, you can automatically display hyperlinks and email addresses on web pages. You are welcome to download the source code of this example and watch the actual effect on the forum at http://www.bizsofts.com . (Author's note: The English version of this article has been published on CodePoject and CodeGuru)