09/10/2018, 17:40
[Q] Fucntion convert url to link ?
Bác nào viet dùm em với . làm sao để khi gõ http://cáigìđó.gìđó thi nó sẽ thêm vào thẻ <a href://....></a> hả các bác
Bài liên quan





function editData($input) {
$input = eregi_replace(" (www.+\\..)"," http://\\1",$input);
$input = eregi_replace("***91;***91;:alpha:***93;***93;+://***91;^<>***91;:space:***93;***93;+***91;***91;:alnum:***93;/***93;","<a href=\"\\0\" target=\"_blank\">\\0</a>",$input);
return $input;
}
Bực qué, ko để ý nên cái code bị auto parse URL, he he, giờ thì ngon rồi
P/s: lưu ý là ở cái hàm replace 1, có dấu cách đó nhé.
Sử dụng:
Sử dụng:
function ChkMail(ByVal strToFormat) Dim strArray Dim Counter if InStr(1, strToFormat, " ") > 0 Then strArray = Split(Replace(strToFormat, "<br />", " <br />", 1, -1, vbTextCompare), " ", -1) 'ChkMail = strArray(0) for Counter = 0 to UBound(strArray) If (InStr(strArray(Counter), "@") > 0) and _ not(InStr(UCase(strArray(Counter)), "MAILTO:") > 0) and _ not(InStr(UCase(strArray(Counter)), "FTP:") > 0) and _ not(InStr(UCase(strArray(Counter)), "[URL") > 0) then ChkMail = ChkMail & " " & edit_hrefs(strArray(counter), 4) else ChkMail = ChkMail & " " & strArray(counter) end if next ChkMail = Replace(ChkMail, " <br />", "<br />", 1, -1, vbTextCompare) else if (InStr(strToFormat, "@") > 0) and _ not(InStr(UCase(strToFormat), "MAILTO:") > 0) and _ not(InStr(UCase(strToFormat), "FTP:") > 0) and _ not(InStr(UCase(strToFormat), "[URL") > 0) then ChkMail = ChkMail & " " & edit_hrefs(strToFormat, 4) else ChkMail = strToFormat end if end if end function function ChkURLs(ByVal strToFormat, ByVal sPrefix, ByVal iType) Dim strArray Dim Counter ChkURLs = strToFormat if InStr(1, strToFormat, sPrefix) > 0 Then strArray = Split(strToFormat, sPrefix, -1) ChkURLs = strArray(0) for Counter = 1 To UBound(strArray) if ((strArray(Counter-1) = "" Or Len(strArray(Counter-1)) < 5) And strArray(Counter)<> "") then ChkURLs = ChkURLs & edit_hrefs(sPrefix & strArray(Counter), iType) elseif ((UCase(Right(strArray(Counter-1), 6)) <> "HREF=""") and _ (UCase(Right(strArray(Counter-1), 5)) <> "[IMG]") and _ (UCase(Right(strArray(Counter-1), 5)) <> "[URL]") and _ (UCase(Right(strArray(Counter-1), 6)) <> "[URL=""") and _ (UCase(Right(strArray(Counter-1), 6)) <> "FTP://") and _ (UCase(Right(strArray(Counter-1), 8)) <> "FILE:///") and _ (UCase(Right(strArray(Counter-1), 7)) <> "HTTP://") and _ (UCase(Right(strArray(Counter-1), 8)) <> "HTTPS://") and _ (UCase(Right(strArray(Counter-1), 5)) <> "SRC=""") and _ (strArray(Counter) <> "")) then ChkURLs = ChkURLs & edit_hrefs(sPrefix & strArray(Counter), iType) else ChkURLs = ChkURLs & sPrefix & strArray(Counter) end if next end if end function <script language="javascript1.2" runat="server"> function edit_hrefs(sURL, iType) { sOutput = new String(sURL); if (iType == 1) { sOutput = sOutput.replace(/\b(http\:\/\/[\w+\.]+[\w+\.\:\/\@\_\?\=\&\-\'\#\%\~\;\,\$\!\+\*]+)/gi, "<a href=\"$1\" target=\"_blank\">$1<\/a>"); } else if (iType == 2) { sOutput = sOutput.replace(/\b(https\:\/\/[\w+\.]+[\w+\.\:\/\@\_\?\=\&\-\'\#\%\~\;\,\$\!\+\*]+)/gi, "<a href=\"$1\" target=\"_blank\">$1<\/a>"); } else if (iType == 3) { sOutput = sOutput.replace(/\b(www\.[\w+\.\:\/\@\_\?\=\&\-\'\#\%\~\;\,\$\!\+\*]+)/gi, "<a href=\"http://$1\" target=\"_blank\">$1<\/a>"); } else if (iType == 4) { sOutput = sOutput.replace(/\b([\w+\-\'\#\%\.\_\,\$\!\+\*]+@[\w+\.?\-\'\#\%\~\_\.\;\,\$\!\+\*]+\.[\w+\.?\-\'\#\%\~\_\.\;\,\$\!\+\*]+)/gi, "<a href=\"mailto\:$1\">$1<\/a>"); } else if (iType == 5) { sOutput = sOutput.replace(/\b(ftp\:\/\/[\w+\.]+[\w+\.\:\/\@\_\?\=\&\-\'\#\%\~\;\,\$\!\+\*]+)/gi, "<a href=\"$1\" target=\"_blank\">$1<\/a>"); } else if (iType == 6) { sOutput = sOutput.replace(/\b(file\:\/\/\/[\w+\:\/\\]+[\w+\/\w+\.\:\/\\\@\_\?\=\&\-\'\#\%\~\;\,\$\!\+\*]+)/gi, "<a href=\"$1\" target=\"_blank\">$1<\/a>"); } return sOutput; } </script>vd : mình có function makecode để định dạng khi user dùng {code } { /code} và function formatstring giống như trên , làm sau mà đoạn ở giữa {code } { /code } không bị format bởi function formatstring ?? Mỉnh có xem trong snitz nhưng không rõ lắm vì ở file topic.asp , snitz vẫn dùng formatstring(RS(...)) bình thường , không biết là snitz xử lý vụ này ở đâu ?
{code}{/code} rõ ràng có "quyền" lớn hơn là {url}{/url} hay là auto-url (tạm gọi cái tự thêm link như thế)
Vì thế khi xử lý xâu, {code} và {/code} sẽ được gọi xử lý trước.
Giả sử bạn tìm được một cặp {code} và {/code}
--> Xâu ban đầu sẽ bị chia ra làm 3 phần (mình coi là s1, s2, s3, trong đó s2 bắt đầu là {code} và kết thúc là {/code})
Nếu mình tạo một hàm formatstring như sau:
Sub FormatString(s, auto) '.... If auto then 'thực hiện auto-link End if '.... Énd Subs = FormatString(s1, true) & FormatString(s2, false) & FormatString(s3, true)
Lưu ý là trong s3 vẫn có thể có {code} {/code} nên thủ tục bạn xây dựng nên là đệ qui.
Sub FS(s, auto) Dim s1, s2, s3 'tìm {code}{/code} If tim_thấy then 'vì tìm thấy nên ta có s1, s2, s3 FS = FS(s1, true) & FS(s2, false) & FS(s3, true) Else 'Không tìm thấy {code}{/code} If auto then s = CheckURL(s)'kiểm tra auto như của snitz End if FS = CheckTag(s) 'kiểm tra các tag như {b}{/b}, {u}{/u} end if End Sub