有没有那么一个函数,会自动转化正文中的url?

老譚山菜 2003-02-27 11:37:46
如题!

在线等!

马上结帖!
...全文
35 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
gOODiDEA 2003-03-01
  • 打赏
  • 举报
回复
gz
plife 2003-03-01
  • 打赏
  • 举报
回复
UP
huweighost 2003-03-01
  • 打赏
  • 举报
回复
请使用正规表达式来匹配你需要格式化的字符串。
正规表示式的匹配字符串为“http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?”
只要使用该功能就可以将你正文中的http地址全部匹配为你需要的格式。
匹配email的匹配字符串是:"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
该功能既是目前论坛等系统常用的url自动解析功能。
xd123 2003-03-01
  • 打赏
  • 举报
回复
好像没有这样的函数吧,就像楼上说的那样,最好自己写一个
  • 打赏
  • 举报
回复
up
webdiyer 2003-02-28
  • 打赏
  • 举报
回复
User Tips: Converting URLs into Hyperlinks


--------------------------------------------------------------------------------

By Wallace

I just finished reading Designing Active Server Pages and was inspired by the regular expressions section. I would like to contribute some code... two functions which are fresh out of the oven!

These functions are to cater for web content stored in a database where URLs & email address are part of the content. The function uses Regular Expression to search and replace URLs and Email address and turn them into viable hyperlinks...

The main function, InsertHyperlinks, has the following definition:

InsertHyperlinks(inText)


The function returns a modified version of inText, replacing all URLs and email addresses with hyperlinks! The needed code is displayed below... be sure to give the live demo a whirl too! (To learn more about regular expressions (which the functions below use heavily) check out the: Regular Expressions Article Index'----------------------------------------------
' InsertHyperlinks(inText)
' Returns a inText with "<a href="URL" target="_BLANK">URL</a>"
' inserted where there is URL found.
'
' URL can start with "www" or "http"
' or
' URL can be a email address "*@*"
'----------------------------------------------
Function InsertHyperlinks(inText)
Dim objRegExp, strBuf
Dim objMatches, objMatch
Dim Value, ReplaceValue, iStart, iEnd

strBuf = ""
iStart = 1
iEnd = 1
Set objRegExp = New RegExp

objRegExp.Pattern = "\b(www|http|\S+@)\S+\b" ' Match URLs and emails
objRegExp.IgnoreCase = True ' Set case insensitivity.
objRegExp.Global = True ' Set global applicability.
Set objMatches = objRegExp.Execute(inText)
For Each objMatch in objMatches
iEnd = objMatch.FirstIndex
strBuf = strBuf & Mid(inText, iStart, iEnd-iStart+1)
If InStr(1, objMatch.Value, "@") Then
strBuf = strBuf & GetHref(objMatch.Value, "EMAIL", "_BLANK")
Else
strBuf = strBuf & GetHref(objMatch.Value, "WEB", "_BLANK")
End If
iStart = iEnd+objMatch.Length+1
Next
strBuf = strBuf & Mid(inText, iStart)
InsertHyperlinks = strBuf
End Function


Function GetHref(url, urlType, Target)
Dim strBuf

strBuf = "<a href="""
If UCase(urlType) = "WEB" Then
If LCase(Left(url, 3)) = "www" Then
strBuf = "<a href=""http://" & url & """ Target=""" & _
Target & """>" & url & "</a>"
Else
strBuf = "<a href=""" & url & """ Target=""" & _
Target & """>" & url & "</a>"
End If
ElseIf UCase(urlType) = "EMAIL" Then
strBuf = "<a href=""mailto:" & url & """ Target=""" & _
Target & """>" & url & "</a>"
End If

GetHref = strBuf

End Function


[View a live demo!]

For an alternative way to accomplish this, check out: URL Linker Code Sample. This example uses VBScript string functions, not Regular Expressions...

Happy Programming!

sandy2001 2003-02-27
  • 打赏
  • 举报
回复
to dillontam(0)(0);
难道HyperLink控件不能满足你的要求吗?
老譚山菜 2003-02-27
  • 打赏
  • 举报
回复
To:toptry(sunny)
对,我就是希望有一个这样的函数能够从纯文本内容中检索出类似于
“http://www.csdn.net”或者“www.csdn.net”字串,然后将其转换成<a href="http://www.csdn.net">...</a>。
toptry 2003-02-27
  • 打赏
  • 举报
回复
如果是纯文本格式的字串,是可行的,
至于是否有函数我不知道,
不过你可以自己写。
toptry 2003-02-27
  • 打赏
  • 举报
回复
是不是要自动从文本中找到类似于:
“http://www.csdn.net”或者“www.csdn.net”字串,
并将其转换成<a href="http://www.csdn.net">...</a>吗?
sandy2001 2003-02-27
  • 打赏
  • 举报
回复
用HyperLink控件
HyperLink1.NavigateUrl="www.csdn.net";
webdiyer 2003-02-27
  • 打赏
  • 举报
回复
用RegularExpression可以,看这个:

Converting URLs into Hyperlinks

http://4guysfromrolla.com/webtech/tips/t110900-1.shtml
老譚山菜 2003-02-27
  • 打赏
  • 举报
回复
例如:在某一段文字中包含有字符串:“http://www.csdn.net”或者“www.csdn.net”,是否有函数可以自动把它们转化为超连接的显示方式。
sandy2001 2003-02-27
  • 打赏
  • 举报
回复
什麽意思啊?
不知道System.Web.HttpUtility.UrlEncode(URL)
是不是你想要的?
老譚山菜 2003-02-27
  • 打赏
  • 举报
回复
to webdiyer(webdiyer)
很遗憾,到目前为止我都没有能够打开你提供的连接,
能否把文章帖过来?感激涕零!
老譚山菜 2003-02-27
  • 打赏
  • 举报
回复
补充一点:我并不知道那一大段内容中是否有
类似于“http://www.csdn.net”这样的字符串,
所以必须有一个函数能判断其中是否含有这样的字符串,
并能自动检索出来,然后进行转化。
老譚山菜 2003-02-27
  • 打赏
  • 举报
回复
to sandy2001(小帅猪)
用HyperLink控件的前提是要先知道该字符串可以显示为超连接,
而现在我需要首先从一大段内容中先找出那个字符串来呀。

62,253

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧