webForumDet fria alternativet

Min funktion funkar delvis...

ASP

10 svar · 256 visningar · startad av jlee

Medlem sedan dec. 2002378 inlägg
Frågan#1

Använder en funktion till ett administrations system för att admin ska kunna ladda upp tex 1.jpg och skriva bild1 som ersätts med bild. Problemet är att tex 13.jpg som är lika med bild13 tyvärr blir bild1 i texten eftersom den läser av bild1(3) då.
Borde inte funktionen funka som den är eller vet någon hur jag borde gjort?
Vill ju kunna använda upp till 100 bilder...
Funktionen:

<%
Function bild(txt)
txt = Replace(txt, "bild1", "<img src=""1.jpg"">")
txt = Replace(txt, "bild2", "<img src=""2.jpg"">")
txt = Replace(txt, "bild3", "<img src=""3.jpg"">")
txt = Replace(txt, "bild4", "<img src=""4.jpg"">")
txt = Replace(txt, "bild5", "<img src=""5.jpg"">")
txt = Replace(txt, "bild6", "<img src=""6.jpg"">")
txt = Replace(txt, "bild7", "<img src=""7.jpg"">")
txt = Replace(txt, "bild8", "<img src=""8.jpg"">")
txt = Replace(txt, "bild9", "<img src=""9.jpg"">")
txt = Replace(txt, "bild10", "<img src=""10.jpg"">")
txt = Replace(txt, "bild11", "<img src=""11.jpg"">")
txt = Replace(txt, "bild12", "<img src=""12.jpg"">")
txt = Replace(txt, "bild13", "<img src=""13.jpg"">")
txt = Replace(txt, "bild14", "<img src=""14.jpg"">")
txt = Replace(txt, "bild15", "<img src=""15.jpg"">")
txt = Replace(txt, "bild16", "<img src=""16.jpg"">")
txt = Replace(txt, "bild17", "<img src=""17.jpg"">")
txt = Replace(txt, "bild18", "<img src=""18.jpg"">")
txt = Replace(txt, "bild19", "<img src=""19.jpg"">")
txt = Replace(txt, "bild20", "<img src=""20.jpg"">")
txt = Replace(txt, "bild21", "<img src=""21.jpg"">")
txt = Replace(txt, "bild22", "<img src=""22.jpg"">")
txt = Replace(txt, "bild23", "<img src=""23.jpg"">")
txt = Replace(txt, "bild24", "<img src=""24.jpg"">")
txt = Replace(txt, "bild25", "<img src=""25.jpg"">")
txt = Replace(txt, "bild26", "<img src=""26.jpg"">")
txt = Replace(txt, "bild27", "<img src=""27.jpg"">")
txt = Replace(txt, "bild28", "<img src=""28.jpg"">")
txt = Replace(txt, "bild29", "<img src=""29.jpg"">")
txt = Replace(txt, "bild30", "<img src=""30.jpg"">")
txt = Replace(txt, "bild31", "<img src=""31.jpg"">")
txt = Replace(txt, "bild32", "<img src=""32.jpg"">")
txt = Replace(txt, "bild33", "<img src=""33.jpg"">")
txt = Replace(txt, "bild34", "<img src=""34.jpg"">")
txt = Replace(txt, "bild35", "<img src=""35.jpg"">")
txt = Replace(txt, "bild36", "<img src=""36.jpg"">")
txt = Replace(txt, "bild37", "<img src=""37.jpg"">")
txt = Replace(txt, "bild38", "<img src=""38.jpg"">")
txt = Replace(txt, "bild39", "<img src=""39.jpg"">")
txt = Replace(txt, "bild40", "<img src=""40.jpg"">")
bild = txt
End Function
%>

Medlem sedan dec. 200012 464 inlägg
#2

Ändra på ordningen, dvs bild40 överst och bild1 nederst

Medlem sedan dec. 2002378 inlägg
#3

väljer en funktion det som står först i ordningen då? så om jag vänder på allt så skulle alla 40 funka och 100 st om jag lägger till?

Medlem sedan jan. 2000978 inlägg
#4

Varför inte bara ta bort de 4 första tecknen, (bild) och använda siffran på slutet till filnamnet?

Medlem sedan dec. 20003 887 inlägg
#5
Function bild(strText)
  Set objRX = New Regex
  
  objRX.Global = True
  objRX.IgnoreCase = True
  objRX.Pattern = "bild(\d+)"
  
  bild = objRX.Replace(strText, "<img src=""$1.jpg"">")
End Function
Medlem sedan dec. 2002378 inlägg
#6

Ska testa förslagen....återkommer senare!

Medlem sedan dec. 2002378 inlägg
#7

ska detta:
bild = objRX.Replace(strText, "<img src=""$1.jpg"">")

mosvara:
txt = Replace(txt, "bild1", "<img src=""1.jpg"">")

Ska jag skriva:
bild1 = objRX.Replace(strText, "<img src=""$1.jpg"">")

fattar inte hur jag ska tillämpa?!

Medlem sedan jan. 2000978 inlägg
#8

Han missade en bokstav

<%
Function bild(strText)
  Set objRX = New Regexp  '***** Här försvann det ett P
  
  objRX.Global = True
  objRX.IgnoreCase = True
  objRX.Pattern = "bild(\d+)"
  
  bild = objRX.Replace(strText, "<img src=""$1.jpg"">")
End Function
response.write(bild("bild55"))

%>

Ger HTML-koden: <img src="55.jpg">

Medlem sedan dec. 2002378 inlägg
#9

Använde funktionen så här:
<%
Function bild(strText)
Set objRX = New Regexp
objRX.Global = True
objRX.IgnoreCase = True
objRX.Pattern = "bild(\d+)"

bild = objRX.Replace(strText, "<img src=""$1.jpg"">")
bild = objRX.Replace(strText, "<img src=""$15.jpg"">")

End Function
%>

..men då funkade bild1 medan bild15 letade efter 155.jpg, varför vet jag inte??
om jag gjort som ovan i koden så lägger väl koden automatiskt till siffran efter?

Medlem sedan mars 20015 287 inlägg
#10

Men... varför har du lagt till den understa raden i funktionen?

Du ska ju kalla på funktionen (i sitt orginalutförande) med:

response.write bild(txt)

Där variabeln txt allts innehåller hela den textmassa vari dina "förkortningar" dyker upp.

Medlem sedan dec. 2002378 inlägg
#11

Tack alla, funktionen löste problemet!!

278 ms totalt · 4 externa anrop · v20260731065814-full.86ec41c2
144 ms — deklarationer (db)
0 ms — hämta statistik (cache)
131 ms — hämta tråd, inlägg och bilagor (db)
138 ms — ändringar (db)