word wrapping in php comment script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rotorio
    New Member
    • Dec 2008
    • 6

    word wrapping in php comment script

    Hi, I am new to php and I am trying to edit one free comment script to fit my needs. The last thing left is to make a word wrapping in comment <div>. It musn't be hard but because I lack understanding in it i have failed to do it myself. I have tried to use many functions but any of them did a job. I must have used them in the wrong way. This is the link of full script code: Comment Form Script Tutorial (It's free too!)

    Here is my code:

    Code:
    echo "
    <link rel=\"stylesheet\" type=\"text/css\" href=\"comments.css\"/>
    ";
    
    $commentquery = mysql_query("SELECT * FROM comments WHERE tutorialid='$tutid' ORDER BY date DESC") or die(mysql_error());
    $commentNum = mysql_num_rows($commentquery);
    
    echo "<div id=\"currentcomments\" class=\"submitcomment\"><h3 class=\"formtitle\">Comments</h3>\n";
    echo "<p class=\"top\">Total: $commentNum (<a href=\"#post\" class=\"link\">comment</a>)</p>\n";
    
    $number = mysql_num_rows($commentquery)+1;
    while($commentrow = mysql_fetch_row($commentquery)){
    $number--;
    
    $commentbb = BBCode($commentrow[3]);
    $commentDate = formatDate($commentrow[4]);
    
    echo "<div class=\"commentbody\" id=\"$commentrow[0]\">\n
    <p>$commentbb</p>\n
    <p class=\"postedby\">";
    if($commentrow[2]){
    
    echo "<b class=\"name\">$commentrow[2]<b class=\"kb\">,</b></b>";
    }
    echo "<b class=\"date\">$commentDate</b> | <b class=\"number\">#$number</b></p>\n
    \n</div>";
    
    }
    echo "</div>";
    }
    Any kind of answers are appreciated. Just don't ignore my post if u have something to say:)

    Thanks in advance.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, rotorio.

    The browser should automatically wrap the content to fit the width of the div.

    Comment

    • rotorio
      New Member
      • Dec 2008
      • 6

      #3
      I must have wrong described my problem. I mean if the word is very long that it runs out of div width, I want it to be wrapped to the next line. To be cut in two pieces. Yes it wraps short words automatically:)

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        That gets trickier.

        There is no standard method to do this using HTML and CSS2 alone. There are some proprietary styles that work in some browsers, but none that will work on all the major browsers.

        You can do this using PHP, however. Check out the wordwrap function. That should add line-breaks every x number of chars.

        If you don't like that, you could also just search strings for words that are to long and split only those words. It will probably eat up a lot more resources than the wordwrap method, but it might look a bit better.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          one thing that should work in modern browsers is the "soft hyphen".

          html: &#38;shy;
          unicode: &#38;#173;

          if the word is too long for the div, the word is broken at the farthest soft hyphen inside the given width.

          regards

          example (there are no hard hyphens in it):
          Dampf&#173;schi f&#173;fahrts&# 173;kapitäns&#1 73;mützen&#173; abzeichen&#173; annäher&#173;be aufsichtigungs& #173;agentur&#1 73;betriebs&#17 3;rats&#173;vor sitzender&#173; gehalts&#173;ab rechnungs&#173; lohn&#173;steue r&#173;berechnu ngs&#173;quotie nt
          (that word doesn't make any sense, but it's very long for demonstration)

          Comment

          • rotorio
            New Member
            • Dec 2008
            • 6

            #6
            I have tried to use this function before:

            Code:
            <?php
            $text = "A very long woooooooooooord.";
            $newtext = wordwrap($text, 8, "\n", true);
            
            echo "$newtext\n";
            ?>
            But it worked not properly. The result was messy. The text was wrapped but the original text which was not wrapped was still here. Also it was wrapped in the wrong line. Besides, every line was left margined more and more. Here is the picture of how it looked:

            Unlimited space to host images, easy to use image uploader, albums, photo hosting, sharing, dynamic image resizing on web and mobile.


            Maybe I inserted the code in wrong lines? Help me someone who knows better where is my problem?

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              I'm not sure why it would increase the margin like that, but I'm pretty sure that has nothing to do with the wordwrap function. Sounds more like your nesting HTML elements that use the same CSS class.

              If the original text was also being displayed, then you must have been echoing it somewhere, which you obviously should not be doing.

              Also, keep in mind that the \n character will NOT be rendered as a line-break in your HTML. You should use the <br> tag (or <br />, if your using XHTML) to add a line-break to your HTML layout.

              For example:
              [code=php]
              <?php
              $text = "123456789" ;
              echo wordwrap($text, 3, "<br />", true);
              ?>
              [/code]
              This would be render like this by a browser:
              Code:
              123
              456
              789

              Comment

              • rotorio
                New Member
                • Dec 2008
                • 6

                #8
                Is there any possibility to make a limit of charackters for every word?

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Originally posted by rotorio
                  Is there any possibility to make a limit of charackters for every word?
                  Everything is possible.
                  Although I fail to see why you would want to do this. Could you elaborate?

                  In any case, the String functions should provide you with the tools to do this.
                  The explode, wordwrap and implode functions should do the trick.

                  Comment

                  Working...