473,507 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

word wrapping in php comment script

6 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. echo "
  2. <link rel=\"stylesheet\" type=\"text/css\" href=\"comments.css\"/>
  3. ";
  4.  
  5. $commentquery = mysql_query("SELECT * FROM comments WHERE tutorialid='$tutid' ORDER BY date DESC") or die(mysql_error());
  6. $commentNum = mysql_num_rows($commentquery);
  7.  
  8. echo "<div id=\"currentcomments\" class=\"submitcomment\"><h3 class=\"formtitle\">Comments</h3>\n";
  9. echo "<p class=\"top\">Total: $commentNum (<a href=\"#post\" class=\"link\">comment</a>)</p>\n";
  10.  
  11. $number = mysql_num_rows($commentquery)+1;
  12. while($commentrow = mysql_fetch_row($commentquery)){
  13. $number--;
  14.  
  15. $commentbb = BBCode($commentrow[3]);
  16. $commentDate = formatDate($commentrow[4]);
  17.  
  18. echo "<div class=\"commentbody\" id=\"$commentrow[0]\">\n
  19. <p>$commentbb</p>\n
  20. <p class=\"postedby\">";
  21. if($commentrow[2]){
  22.  
  23. echo "<b class=\"name\">$commentrow[2]<b class=\"kb\">,</b></b>";
  24. }
  25. echo "<b class=\"date\">$commentDate</b> | <b class=\"number\">#$number</b></p>\n
  26. \n</div>";
  27.  
  28. }
  29. echo "</div>";
  30. }
  31.  
Any kind of answers are appreciated. Just don't ignore my post if u have something to say:)

Thanks in advance.
Dec 26 '08 #1
8 2173
pbmods
5,821 Recognized Expert Expert
Heya, rotorio.

The browser should automatically wrap the content to fit the width of the div.
Dec 26 '08 #2
rotorio
6 New Member
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:)
Dec 26 '08 #3
Atli
5,058 Recognized Expert Expert
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.
Dec 26 '08 #4
Dormilich
8,658 Recognized Expert Moderator Expert
one thing that should work in modern browsers is the "soft hyphen".

html: &shy;
unicode: &#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­schif­fahrts­kapitäns­mützen­abzeichen­annäh er­beaufsichtigungs­agentur­betriebs­rats­vorsitze nder­gehalts­abrechnungs­lohn­steuer­berechnungs­q uotient
(that word doesn't make any sense, but it's very long for demonstration)
Dec 26 '08 #5
rotorio
6 New Member
I have tried to use this function before:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $text = "A very long woooooooooooord.";
  3. $newtext = wordwrap($text, 8, "\n", true);
  4.  
  5. echo "$newtext\n";
  6. ?>
  7.  
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:

http://img243.imageshack.us/my.php?image=testaslq7.jpg

Maybe I inserted the code in wrong lines? Help me someone who knows better where is my problem?
Dec 27 '08 #6
Atli
5,058 Recognized Expert Expert
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $text = "123456789";
  3. echo wordwrap($text, 3, "<br />", true);
  4. ?>
  5.  
This would be render like this by a browser:
Expand|Select|Wrap|Line Numbers
  1. 123
  2. 456
  3. 789
  4.  
Dec 27 '08 #7
rotorio
6 New Member
Is there any possibility to make a limit of charackters for every word?
Dec 27 '08 #8
Atli
5,058 Recognized Expert Expert
@rotorio
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.
Dec 27 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
7398
by: Clifford W. Racz | last post by:
Has anyone solved the issue of translating lists in Word 2003 (WordML) into xHTML? I have been trying to get the nested table code for my XSLT to work for a while now, with no way to get the...
11
5580
by: yawnmoth | last post by:
word wrapping normally treats some spaces as line feeds, if there hasn't been a line feed for quite a while. so while a string with eighty consecutive a's might not word wrap, a space placed...
5
5277
by: nimdez | last post by:
Hi, I am working on an existing code base in which a lot of data displayed to the user is formatted in tables. Most tables are printed row-by-row using printf() with "%s" print conversion...
10
2308
by: Douglas G | last post by:
I've tried various ideas on this problem, but I don't see word wrapping. Can you point out what is wrong? It's a K&R exercise, and I'm still new to programming. Other pointers would be helpful...
4
1996
by: Jake Barnes | last post by:
I'm reading over this page: http://wiki.script.aculo.us/scriptaculous/show/Usage I'm struck by this code example +++++++++++++++++++++++++++++++ 3. Use
1
1969
by: tonten | last post by:
I've been searching for a while now for a solution to the firefox word wrapping issue. I found a solution here @ http://javascript.about.com/library/blspline.htm I followed the instructions...
2
19723
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
0
2061
by: Francesco Pietra | last post by:
I forgot to add that the lines to strip are in present case of the type of the following block HETATM 7007 O WAT 446 27.622 34.356 55.205 1.00 0.00 O HETATM 7008 H1 WAT...
9
4239
by: DeZZar | last post by:
Hi all, Basically I want the data in an open form to merge with a word template and create a new document. So a user navigates to a particular records and presses a button "Produce Document"...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.