Avatar billede mortency Nybegynder
09. maj 2005 - 12:31 Der er 6 kommentarer og
2 løsninger

Legge inn <br> etter x antall tegn

Hvordan kan jeg lage en funksjon som sjekker lengden på en string og legger inn <br> etter en bestemt lengde?

Den skal dog ikke bryte mitt i noen ord.

Eks:
str = "Dette er en test som skal legge inn br...."

Ska bli:

Dette
er en
test
som
skal
legge
inn br
...

AddBR(str,5)

Function addBR

End function
Avatar billede hiks Nybegynder
09. maj 2005 - 12:34 #1
hey

jeg tror det du leder efter (dog uden helt at vide det...) er en wordwrapper. Du kan kigge på dette script og spørge, hvis du vil have lidt hjælp til det. Du kan evt. udbygge den funktion til at du kan bestemme hvilket "tegn" (<br>,<p> eller lign.) der skal wrappes ved hjælp af...

http://www.asp101.com/samples/viewasp.asp?file=wordwrap.asp

/hiks
Avatar billede mortency Nybegynder
09. maj 2005 - 12:57 #2
Hei scriptet virker fint det men jeg trenger litt hjelp med det.

<%
' Declare the variables to store the original text and the
' text after we word wrap it.
Dim strTextPreWrap
Dim strTextPostWrap

' Here's the text we're going to be using for illustration.
strTextPreWrap = "The quick brown fox jumps over the lazy dog.  " _
    & "The quick brown fox jumps over the lazy dog again.  " _
    & "The quick brown fox jumps over the lazy dog one more time."

' Pass the text to our Wrapping function and save the result
' to the new variable.  I'm using the number 40 as the default.
' I'm not going to build a form and all that stuff so that the
' code stays simple, but I will let you enter a number on the
' querystring if you want to play around with different values.

' Notice I've used <p><code></code></p> for display.  This gives
' you the mono-spaced type, but doesn't word wrap at vbCrLf
' characters.  I've used it instead of a <pre></pre> because I
' didn't want the original line to scroll off to the right in
' the browser.  As such I had to compensate and convert the
' vbCrLf's to <br />'s so you could see the result in the browser.
' The call to AddBrToCrLf isn't needed when outputting text
' to a non-HTML environment like an email or text file and it
' doesn't do anything on the non-wrapped text, but I used it on
' both so no one accused me of doing anything funny in it.

' Here's the function that does the work.  It takes a string
' to word wrap and a maximum line length and returns the
' string wrapped appropriately.
Function WordWrap(strTextToBeWrapped, intMaxLineLength)
    Dim strWrappedText          ' Result storage

    Dim intLengthOfInput        ' Length of original

    Dim intCurrentPosition      ' Where we're at now
    Dim intCurrentLineStart      ' Where the current line starts
    Dim intPositionOfLastSpace  ' Last space we saw

    ' Get this once so we don't have to keep checking
    intLengthOfInput = Len(strTextToBeWrapped)

    ' Start both of these at the beginning
    intCurrentPosition = 1
    intCurrentLineStart = 1

    ' Loop through until we get to the end
    Do While intCurrentPosition < intLengthOfInput
          ' If the current position is a space, make a note of
          ' it's location for later use.
          If Mid(strTextToBeWrapped, intCurrentPosition, 1) = " " Then
              intPositionOfLastSpace = intCurrentPosition
          End If

          ' If we're at what should be the end of a line, we go back
          ' to the last space we saw and cut the line there.
          If intCurrentPosition = intCurrentLineStart + intMaxLineLength Then
              ' Some debugging lines if something's not lining up.
              'Response.Write intCurrentLineStart & "<br />"
              'Response.Write intPositionOfLastSpace & "<br />"
              'Response.Write Trim(Mid(strTextToBeWrapped, intcurrentLineStart, _
              '    intPositionOfLastSpace - intCurrentLineStart + 1)) & "<br />"

              ' Append this latest line to our result
              strWrappedText = strWrappedText _
                    & Trim(Mid(strTextToBeWrapped, intcurrentLineStart, _
                    intPositionOfLastSpace - intCurrentLineStart + 1)) _
                    & vbCrLf

              ' Reset the next line's starting point to the point we
              ' used for the last one's end + 1.
              intCurrentLineStart = intPositionOfLastSpace + 1

              ' Remove any leading spaces that might mess up our
              ' character count.  If you want to just pull of one,
              ' switch this to a simple If conditional instead of
              ' looping.
              Do While Mid(strTextToBeWrapped, intCurrentLineStart, 1) = " "
                    intCurrentLineStart = intCurrentLineStart + 1
              Loop
          End If

          ' Increment our location indicator.
          intCurrentPosition = intCurrentPosition + 1
    Loop

    ' Since the loop ends before we add the remaining text,
    ' add remaining text as the last line.
    strWrappedText = strWrappedText & Trim(Mid(strTextToBeWrapped, _
          intcurrentLineStart)) & vbCrLf

    ' Return our result to the calling line.
    WordWrap = strWrappedText
   
    'Denne skal skrive ut texten
    Response.Write(Replace(strTextToBeWrapped, vbCrLf, "<br>" & vbCrLf))
End Function

Call WordWrap(strTextPreWrap, 20)
%>
Avatar billede hiks Nybegynder
09. maj 2005 - 14:30 #3
ok - jeg går ud fra at det er kaldet til funktionen du har svært ved?!?

<%
' Here's the function that does the work.  It takes a string
' to word wrap and a maximum line length and returns the
' string wrapped appropriately.
Function WordWrap(strTextToBeWrapped, intMaxLineLength)
    Dim strWrappedText          ' Result storage

    Dim intLengthOfInput        ' Length of original

    Dim intCurrentPosition      ' Where we're at now
    Dim intCurrentLineStart      ' Where the current line starts
    Dim intPositionOfLastSpace  ' Last space we saw

    ' Get this once so we don't have to keep checking
    intLengthOfInput = Len(strTextToBeWrapped)

    ' Start both of these at the beginning
    intCurrentPosition = 1
    intCurrentLineStart = 1

    ' Loop through until we get to the end
    Do While intCurrentPosition < intLengthOfInput
          ' If the current position is a space, make a note of
          ' it's location for later use.
          If Mid(strTextToBeWrapped, intCurrentPosition, 1) = " " Then
              intPositionOfLastSpace = intCurrentPosition
          End If

          ' If we're at what should be the end of a line, we go back
          ' to the last space we saw and cut the line there.
          If intCurrentPosition = intCurrentLineStart + intMaxLineLength Then
              ' Some debugging lines if something's not lining up.
              'Response.Write intCurrentLineStart & "<br />"
              'Response.Write intPositionOfLastSpace & "<br />"
              'Response.Write Trim(Mid(strTextToBeWrapped, intcurrentLineStart, _
              '    intPositionOfLastSpace - intCurrentLineStart + 1)) & "<br />"

              ' Append this latest line to our result
              strWrappedText = strWrappedText _
                    & Trim(Mid(strTextToBeWrapped, intcurrentLineStart, _
                    intPositionOfLastSpace - intCurrentLineStart + 1)) _
                    & vbCrLf

              ' Reset the next line's starting point to the point we
              ' used for the last one's end + 1.
              intCurrentLineStart = intPositionOfLastSpace + 1

              ' Remove any leading spaces that might mess up our
              ' character count.  If you want to just pull of one,
              ' switch this to a simple If conditional instead of
              ' looping.
              Do While Mid(strTextToBeWrapped, intCurrentLineStart, 1) = " "
                    intCurrentLineStart = intCurrentLineStart + 1
              Loop
          End If

          ' Increment our location indicator.
          intCurrentPosition = intCurrentPosition + 1
    Loop

    ' Since the loop ends before we add the remaining text,
    ' add remaining text as the last line.
    strWrappedText = strWrappedText & Trim(Mid(strTextToBeWrapped, _
          intcurrentLineStart)) & vbCrLf

    ' Return our result to the calling line.
    WordWrap = strWrappedText
End Function

Function AddBrToCrLf(strInput)
    AddBrToCrLf = Replace(strInput, vbCrLf, "<br />" & vbCrLf)
End Function

strText = "Dette er en test som skal legge inn br...."
strText = WordWrap(strText, 10)

Response.Write AddBrToCrLf(strText)
%>

/hiks
Avatar billede busschou Praktikant
09. maj 2005 - 20:43 #4
ser lang ud - denne her virker for mig
---
<%
function AddBR(str,sz)
tjek = false
for t=1 to Len(str)
  if (t mod sz) = 0 then
    if Mid(str,t,1) = " " then
      nytekst = nytekst & "<br>"
    else
      tjek = true
      nytekst = nytekst & Mid(str,t,1)
    end if
  else
  if Mid(str,t,1) = " " and tjek then
    nytekst = nytekst & "<br>"
  else
    nytekst = nytekst & Mid(str,t,1)
  end if
  end if
next
AddBR = nytekst
end function
tekst = "her er en lang tekst som skal opdeles"
tekst = AddBR(tekst,5)
response.write tekst
%>
Avatar billede busschou Praktikant
19. juli 2005 - 10:33 #5
mortency >> kommet videre?
Avatar billede mortency Nybegynder
03. oktober 2005 - 13:45 #6
busschou poster du et svar så får dere dele pointene.
Avatar billede busschou Praktikant
03. oktober 2005 - 17:38 #7
oki doki :o)
Avatar billede mortency Nybegynder
04. oktober 2005 - 13:53 #8
Dette fungerte fint, takk for hjelpen begge to.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester