SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help formatting a string of text

    How to I format the following string from

    The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

    to

    The quick brown fox jumps over the lazy dog. The quick brown fox jumps
    over the lazy dog. The quick brown fox jumps over the lazy dog. The quick
    brown fox jumps over the lazy dog.


    so it looks nice and neat and does not scroll off the edge if the ASP.NET (VB) page?
    Last edited by kg1; Jan 9, 2007 at 18:58.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I guess theres a lot of ways you can achieve this. Two of them would be using a table with a fixed width, or css.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This does not work.

    <div style="width: 690px;">
    <&#37; Response.Write(row("content"))%>
    </div>

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    First, decide how many characters you want per line. I'll say 70.
    PHP Code:
    <?php
    $s 
    'The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.';
    $length strlen($s);
    for (
    $i 0$i $length 70$i++) {
      
    $start $i 70;
      
    substr_replace($s"<br>"$start0);
    }
    ?>
    Should work, haven't tested it though. I know nothing about ASP.NET so I don't know if you would want to add <br> or \n. I have to ask though... why not do this with ASP?

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what I use to develop with at work :P

    Only problem with using the above is that it cuts words in two sometimes.

    Thanks or your post.

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ah, didn't think of that.
    PHP Code:
    <?php
    $s 
    'The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.';
    $bits explode(' '$s);
    foreach (
    $bits as $key => $bit) {
      
    $newstr .= $bit[$key];
      
    $nextkey $key 1;
      if (
    strlen($newstr) > 60 && strlen($newstr ' ' $bit[$nextkey]) > 70) {
        
    $newstr .= '<br>';
      } else {
        
    $newstr .= ' ';
      }
    }
    echo 
    $newstr;
    ?>

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <&#37;
    Dim myString As String = "I was walking through the park when I realised I was insane. 'oh my goodness', i said out loud."

    Dim myNewString As New StringBuilder(myString)
    Dim position As Integer = 33
    Dim timesToLoop As Integer = Round(myString.Length / position, 0)
    Dim val As String

    For counter As Integer = 0 To timesToLoop - 1
    Do While Not position > myString.Length
    val = myNewString(position)
    If val = " " Then
    myNewString.Insert(position, "<br />")
    position += 33
    Exit Do
    End If
    position += 1
    Loop
    Next

    Response.Write(myNewString)
    %>

    It works, yippee

  8. #8
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <%
            
    Dim myString As String "I was walking through the park when I realised I was insane. 'oh my goodness', i said out loud."

            
    Dim myNewString As New StringBuilder(myString)
            
    Dim position As Integer 33
            Dim timesToLoop 
    As Integer Round(myString.Length position0)
            
    Dim val As String
     
            
    For counter As Integer 0 To timesToLoop 1
                
    Do While Not position myString.Length
                    val 
    myNewString(position)
                    If 
    val " " Then
                        myNewString
    .Insert(position"<br />")
                        
    position += 33
                        
    Exit Do
                    
    End If
                    
    position += 1
                Loop
            Next
            
            Response
    .Write(myNewString)
    %> 
    It works, yippee

  9. #9
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <%
            
    Dim myString As String "I was walking through the park when I realised I was insane. 'oh my goodness', i said out loud."

            
    Dim myNewString As New StringBuilder(myString)
            
    Dim position As Integer 33
            Dim timesToLoop 
    As Integer Round(myString.Length position0)
            
    Dim val As String
     
            
    For counter As Integer 0 To timesToLoop 1
                
    Do While Not position myString.Length
                    val 
    myNewString(position)
                    If 
    val " " Then
                        myNewString
    .Insert(position"<br />")
                        
    position += 33
                        
    Exit Do
                    
    End If
                    
    position += 1
                Loop
            Next
            
            Response
    .Write(myNewString)
    %> 
    It works, yippee

  10. #10
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's wonderful.

  11. #11
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the php way would be wordwrap(), although i kinda get the feeling you have already seen this function?

  12. #12
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No I haven't seen this function before, however I can follow most of it but am not sure what the explode function does?

  13. #13
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <? // word wrap
    $str wordwrap($str80"\n");
    ?>

  14. #14
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

  15. #15
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    see the manual for wrodwrap() to see why i thought you saw it already lol

  16. #16
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Damn, I didn't knew that existed. Learn something every day.

  17. #17
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    manual is the best practice to getting started ..

  18. #18
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    This one might work a little better

    PHP Code:
    Public Function WordWrap(ByVal strTextToBeWrapped As StringByVal intMaxLineLength As Integer) As String
            Dim strWrappedText 
    As String
            Dim intLengthOfInput 
    As Integer
            Dim intCurrentPosition 
    As Integer
            Dim intCurrentLineStart 
    As Integer
            Dim intPositionOfLastSpace 
    As Integer

            intLengthOfInput 
    Len(strTextToBeWrapped)
            
    intCurrentPosition 1
            intCurrentLineStart 
    1

            
    Do While intCurrentPosition intLengthOfInput
                
    If Mid(strTextToBeWrappedintCurrentPosition1) = " " Then
                    intPositionOfLastSpace 
    intCurrentPosition
                End 
    If

                If 
    intCurrentPosition intCurrentLineStart intMaxLineLength Then
                    strWrappedText 
    strWrappedText Trim(Mid(strTextToBeWrappedintCurrentLineStartintPositionOfLastSpace intCurrentLineStart 1)) & vbCrLf
                    intCurrentLineStart 
    intPositionOfLastSpace 1
                    
    Do While Mid(strTextToBeWrappedintCurrentLineStart1) = " "
                        
    intCurrentLineStart intCurrentLineStart 1
                    Loop
                End 
    If

                
    intCurrentPosition intCurrentPosition 1
            Loop

            strWrappedText 
    strWrappedText Trim(Mid(strTextToBeWrappedintCurrentLineStart)) & vbCrLf
            strWrappedText 
    Replace(strWrappedTextvbCrLf"<br />" vbCrLf)

            Return 
    strWrappedText
    End 
    Function 

  19. #19
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its a shame ASP.NET does not have a prebuilt function! Or does it!!! :P

  20. #20
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i was thinking we r in PHP thread and problem is in PHP as well

  21. #21
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Word Wrap Function

    Here's another function

    PHP Code:
    Public Function WordWrap(ByVal strTextToBeWrapped As StringByVal intMaxLineLength As Integer) As String
            Dim wrappedText 
    As String
            Dim lengthOfInput 
    As Integer
            Dim currentPosition 
    As Integer
            Dim currentLineStart 
    As Integer
            Dim positionOfLastSpace 
    As Integer

            lengthOfInput 
    Len(strTextToBeWrapped)
            
    currentPosition 1
            currentLineStart 
    1

            
    Do While currentPosition lengthOfInput
                
    If Mid(strTextToBeWrappedcurrentPosition1) = " " Then
                    positionOfLastSpace 
    currentPosition
                End 
    If

                If 
    currentPosition currentLineStart intMaxLineLength Then
                    wrappedText 
    wrappedText Trim(Mid(strTextToBeWrappedcurrentLineStartpositionOfLastSpace currentLineStart 1)) & vbCrLf
                    currentLineStart 
    positionOfLastSpace 1
                    
    Do While Mid(strTextToBeWrappedcurrentLineStart1) = " "
                        
    currentLineStart currentLineStart 1
                    Loop
                End 
    If

                
    currentPosition currentPosition 1
            Loop

            wrappedText 
    wrappedText Trim(Mid(strTextToBeWrappedcurrentLineStart)) & vbCrLf
            wrappedText 
    Replace(wrappedTextvbCrLf"<br />" vbCrLf)

            Return 
    wrappedText
    End 
    Function 

  22. #22
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, the problem was in ASP.NET but I wasn't getting any luck in those forums so posted in here for an example in PHP. I would then rewrite the code in ASP.NET


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •