webForumDet fria alternativet

RegExp för ersättning av <img>

ASP

11 svar · 646 visningar · startad av Roosweb

Medlem sedan aug. 200679 inlägg
Frågan#1

Hejsan.
Jag satt och gjorde lite såkallade "BB" koder till ett forum, när jag stötte på ett problem, som att göra texten fet och liknande är inget problem det löste jag såhär:

strText = Replace(strText, "[ b]", "<b>")
strText = Replace(strText, "[ /b]", "</b>")

Men sen kom jag på att jag vill ha en funktion så att man kan skriva

Medlem sedan dec. 20025 483 inlägg
#2
set re = new regexp
re.global = true
re.pattern = "[img=([^]]+)]"
strText = re.replace(strText, "<img src=""$1"" />")
Medlem sedan aug. 200679 inlägg
#3

När jag testar det så får jag bara:

Microsoft VBScript runtime error '800a1399'

Syntax error in regular expression

includes/codes.asp, line 38

Fast jag har redan en del kod där också, så kanske är den som strular lite? Jag är så dålig på RegExp ännu :)

<%
                  
                  Function FixLinks(ByVal strText)
                     With New RegExp
                        .Global=True
                        .IgnoreCase=True
                        .Pattern="(http://)([\w~%&=#:\-/\?\.]*[\w~%&=#:\-/])"
                            strText=.Replace(strText,"<a href=""$1$2"" target=""_blank"">$2</a>")
                            strText = Replace(strText,":)","<img src=""community/Includes/smileys/glad.gif"">")
                            strText = Replace(strText,"><","<img src=""community/Includes/smileys/ahh.gif"">")
                            strText = Replace(strText,":P","<img src=""community/Includes/smileys/P.gif"">")
                            strText = Replace(strText,":(","<img src=""community/Includes/smileys/sur.gif"">")
                            strText = Replace(strText,":-(","<img src=""community/Includes/smileys/grater.gif"">")
                            strText = Replace(strText,";)","<img src=""community/Includes/smileys/blink.gif"">")
                            strText = Replace(strText,":D","<img src=""community/Includes/smileys/superglad.gif"">")
                            strText = Replace(strText,":0","<img src=""community/Includes/smileys/smiley3.gif"">")
                            strText = Replace(strText,":S","<img src=""community/Includes/smileys/smiley5.gif"">")
                            strText = Replace(strText,":@","<img src=""community/Includes/smileys/smiley7.gif"">")

                            strText = Replace(strText,vbcrlf,"<br>")
strText = Replace(strText, "[b]", "<b>")
strText = Replace(strText, "[/b]", "</b>")
strText = Replace(strText, "[c]", "<center>")
strText = Replace(strText, "[/c]", "</center>")
strText = Replace(strText, "[u]", "<u>")
strText = Replace(strText, "[/u]", "</u>")
strText = Replace(strText, "[i]", "<i>")
strText = Replace(strText, "[/i]", "</i>")
strText = Replace(strText, "[red]", "<font color=""#ff0000"">")
strText = Replace(strText, "[/red]", "</font>")
strText = Replace(strText, "[p]", "<p></p>")
strText = Replace(strText, "[br]", "<br />")
strText = Replace(strText, "<embed>", "****")     
strText = Replace(strText, "<script>", "****Jag är en dålig hacker****")
set re = new regexp
re.global = true
re.pattern = "[img=([^]]+)]"
strText = re.replace(strText, "<img src=""$1"" />")
                End With
                         FixLinks=strText
                  End Function

Function Kort(strText,intNumb)
  If Len(strText) > intNumb Then
  Response.Write Left(strText,intNumb)&"..."
  Else
  Response.Write strText
  End If
End Function				  
				  
%>
Medlem sedan dec. 20025 483 inlägg
#4

Oops!

set re = new regexp
re.global = true
re.pattern = "\[img=([^]]+)\]"
strText = re.replace(strText, "<img src=""$1"" />")

:)

Medlem sedan aug. 200679 inlägg
#5

Okej ingen mer felkod i alla fall, men bildfunktionen fungerar inte heller, om inte jag skriver fel då, testade med:

Medlem sedan dec. 20025 483 inlägg
#6

Det fungerar för mig. Problemet ligger i

.Pattern="([url]http://)([/url][\w~%&=#:\-/\?\.]*[\w~%&=#:\-/])"
                            strText=.Replace(strText,"<a href=""$1$2"" target=""_blank"">$2</a>")

...som stoppar på <a></a>-taggar. Resultatet blir typ

<img src="<a href="http://www.roosweb.se/gfx2/roosweb2.jpg" target="_blank">www.roosweb.se/gfx2/roosweb2.jpg</a>" />
Medlem sedan aug. 200679 inlägg
#7

Okej, jag testade ta bort det då fungerar det, vet du hur man kan komma runt detta så jag kan ha båda funktionerna? Så man både kan skriva http:// och att det automatiskt blir en länk och så att [img= fungerar?

Tackar för hjälpen :)

Medlem sedan dec. 20025 483 inlägg
#8

Någonting sådant här kanske

set re = new regexp
re.global = true
re.pattern = "\[img=<a href=""([^""]+)[^]]+\]"
strText = re.replace(strText, "<img src=""$1"" />")

Edit: Skrivs alltid hela sökvägen till bilden ut?

Medlem sedan juni 20019 519 inlägg
#9
Sub FixReg(re,pattern,replace,str)
    re.pattern = pattern
    str = re.replace(str,replace)
End Sub

Function FixLinks(ByVal strText)
  Dim Reg
  set Reg = new RegExp
    Reg.Global=True
    Reg.IgnoreCase=True
'    strText = Replace(strText,":(","<img src=""community/Includes/smileys/sur.gif"">")
'    strText = Replace(strText,":-(","<img src=""community/Includes/smileys/grater.gif"">")
'    strText = Replace(strText,";)","<img src=""community/Includes/smileys/blink.gif"">")
'    strText = Replace(strText,":D","<img src=""community/Includes/smileys/superglad.gif"">")
'    strText = Replace(strText,":0","<img src=""community/Includes/smileys/smiley3.gif"">")
'    strText = Replace(strText,":S","<img src=""community/Includes/smileys/smiley5.gif"">")
'    strText = Replace(strText,":@","<img src=""community/Includes/smileys/smiley7.gif"">")
    strText = Replace(strText, "[p]", "<p></p>")
    FixReg Reg, "(^|\W):\)(?=$|\W)", "$1<img src=""community/Includes/smileys/glad.gif"">", strText
    FixReg Reg, "(^|\W)><(?=$|\W)", "$1<img src=""community/Includes/smileys/ahh.gif"">", strText
    FixReg Reg, "(^|\W):P(?=$|\W)", "$1<img src=""community/Includes/smileys/P.gif"">", strText
    FixReg Reg, "\[c\]", "<center>", strText
    FixReg Reg, "\[\/c\]", "</center>", strText
    FixReg Reg, "\[red\]", "<span style=""color:#f00;"">", strText
    FixReg Reg, "\[\/red\]", "</span>", strText
    FixReg Reg, "\[br\]|" & vbCrLf,"<br />", strText
    FixReg Reg, "\<embed\>","****", strText
    FixReg Reg, "\<script\>","****Jag är en dålig hacker****", strText
    FixReg Reg, "[^\=]([url]http://)([/url][\w~%&=#:\-/\?\.]*[\w~%&=#:\-/])", "<a href=""$1$2"" target=""_blank"">$2</a>", strText
    FixReg Reg, "\[(b|\/b|i|\/u|i|\/i)\]", "<$1>", strText
    FixReg Reg, "\[img=([^]]+)\]","<img src=""$1"" />", strText
  FixLinks=strText
End Function

Fungerade på det jag testade.. (dock kommentarde jag ut några + gjorde om lite i din funktion)... använd vad du vill..

Medlem sedan aug. 200679 inlägg
#10

Tack så sjukt mycket för allt arbete du lade ned på den koden åt mig! Fungerar klockrent, tack så mycket båda två för hjälpen! :)

Medlem sedan juni 20019 519 inlägg
#11

Glöm inte att att avsluta "Reg"

Set Reg  = Nothing
Medlem sedan aug. 200679 inlägg
#12

Redan gjort :) Lite kan jag tänka själv i alla fall hehe ;)

255 ms totalt · 4 externa anrop · v20260731065814-full.1dc6f849
120 ms — deklarationer (db)
0 ms — hämta statistik (cache)
131 ms — hämta tråd, inlägg och bilagor (db)
121 ms — ändringar (db)