473,387 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

wordwrap, chunk_split or something else can help me?

7
Hi all,
How to break line of long string including inside html code?

Example
I have a long string of letters AAAAAAAACCCCCCCCC and they was formated with html code.
Expand|Select|Wrap|Line Numbers
  1. <font color=magenta>AAAAAAAAA</font><font color=red>CCCCCCCCCC</font>
How to break line each 4 letters (still keep the color of these letters). I was used wordwrap, chunk_split but failed. Please help me, thanks!
Dec 16 '09 #1

✓ answered by Atli

Hey.

You could just go through the string manually and add line-breaks at the appropriate positions. You would just have to loop through each character in the string, detect whether or not it is inside a HTML tag, and if it is not, count it. And then you would just add a "<br>" every time the counter reaches 4.

For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $input = "<font color=magenta>AAAAAAAAA</font><font color=red>CCCCCCCCCC</font>";
  3. $output = '';
  4.  
  5. $tag_open = false;
  6. $letter_counter = 0;
  7. $input_length = strlen($input);
  8.  
  9. for($i = 0; $i < $input_length; ++$i) 
  10. {
  11.     // Check if the current letter is a opening or closing tag.
  12.     if($input[$i] == ">" || $input[$i] == "<") 
  13.     {
  14.         // It is! Change the $open boolean so that the next
  15.         // loop knows that a tag open/close was detected.
  16.         $tag_open = !$tag_open;
  17.     }
  18.     else 
  19.     {
  20.         // Check if the current letter is inside a tag.
  21.         if(!$tag_open)
  22.         {
  23.             // Check if the current character count is at 4
  24.             if($letter_counter == 4)
  25.             {
  26.                 // Add a line-break and reset the count
  27.                 $output .= "<br>";
  28.                 $letter_counter = 0;
  29.             }
  30.             // Add the current letter to the cycle count.
  31.             ++$letter_counter;
  32.         }
  33.     }
  34.  
  35.     // Add the current letter to the output string.
  36.     $output .= $input[$i];
  37. }
  38.  
  39. echo $output;
  40. ?>
Prints:
Expand|Select|Wrap|Line Numbers
  1. <font color=magenta>AAAA<br>AAAA<br>A</font><font color=red>CCC<br>CCCC<br>CCC</font>
And also, you should try to avoid using old HTML tags like <font> to format your pages. It's generally better to use CSS.
Expand|Select|Wrap|Line Numbers
  1. # Instead of doing:
  2. <font color="red">ABCD</font>
  3. # Do:
  4. <span style="color: red;">ABCD</span>
Or better yet:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <head>
  3.     <title>CSS Example</title>
  4.     <style type="text/css">
  5.         span.red {
  6.             color: red;
  7.         }
  8.     </style>
  9. </head>
  10. <body>
  11.     <span class="red">ABCD</span>
  12. </body>
  13. </html>

2 3008
Atli
5,058 Expert 4TB
Hey.

You could just go through the string manually and add line-breaks at the appropriate positions. You would just have to loop through each character in the string, detect whether or not it is inside a HTML tag, and if it is not, count it. And then you would just add a "<br>" every time the counter reaches 4.

For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $input = "<font color=magenta>AAAAAAAAA</font><font color=red>CCCCCCCCCC</font>";
  3. $output = '';
  4.  
  5. $tag_open = false;
  6. $letter_counter = 0;
  7. $input_length = strlen($input);
  8.  
  9. for($i = 0; $i < $input_length; ++$i) 
  10. {
  11.     // Check if the current letter is a opening or closing tag.
  12.     if($input[$i] == ">" || $input[$i] == "<") 
  13.     {
  14.         // It is! Change the $open boolean so that the next
  15.         // loop knows that a tag open/close was detected.
  16.         $tag_open = !$tag_open;
  17.     }
  18.     else 
  19.     {
  20.         // Check if the current letter is inside a tag.
  21.         if(!$tag_open)
  22.         {
  23.             // Check if the current character count is at 4
  24.             if($letter_counter == 4)
  25.             {
  26.                 // Add a line-break and reset the count
  27.                 $output .= "<br>";
  28.                 $letter_counter = 0;
  29.             }
  30.             // Add the current letter to the cycle count.
  31.             ++$letter_counter;
  32.         }
  33.     }
  34.  
  35.     // Add the current letter to the output string.
  36.     $output .= $input[$i];
  37. }
  38.  
  39. echo $output;
  40. ?>
Prints:
Expand|Select|Wrap|Line Numbers
  1. <font color=magenta>AAAA<br>AAAA<br>A</font><font color=red>CCC<br>CCCC<br>CCC</font>
And also, you should try to avoid using old HTML tags like <font> to format your pages. It's generally better to use CSS.
Expand|Select|Wrap|Line Numbers
  1. # Instead of doing:
  2. <font color="red">ABCD</font>
  3. # Do:
  4. <span style="color: red;">ABCD</span>
Or better yet:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <head>
  3.     <title>CSS Example</title>
  4.     <style type="text/css">
  5.         span.red {
  6.             color: red;
  7.         }
  8.     </style>
  9. </head>
  10. <body>
  11.     <span class="red">ABCD</span>
  12. </body>
  13. </html>
Dec 16 '09 #2
nhbach
7
You're great! Thank you so much, i can resolve my probleme!
Dec 17 '09 #3

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

Similar topics

5
by: lawrence | last post by:
When users enter urls or other long strings it can destroy the formatting of a page. A long url, posted in a comment, can cause page distortions that make the page unreadable, till the website...
6
by: Pierre Jelenc | last post by:
I need to wrap text with a negative indent, that is this: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad...
0
by: Richard Back | last post by:
Hi, I would like to print the contents of a textbox to a printer, and for the text to wordwrap. The text could be more than one pages worth so I need to track the pages. I can get it to...
0
by: Mike | last post by:
Hi All, I mistakenly just posted this to the VB group. Sorry for the cross-post. I am working with a textbox in C# which pulls a set of lines (stored in the database as text_type,...
1
by: louise raisbeck | last post by:
Hi there, I create a report online with basically a dump of loads of tables. One of them has 27 columns and as such it ignores the datagrid.width property and goes right along the screen in a...
2
by: yxq | last post by:
Hello, The length of Tooltip string is not fixed, how to wordwrap according to width size? Where to find a function to return Multi-lines string from a long string? Thank you
1
by: Cesar Ronchese | last post by:
Hi! I'm using a DataGridView to show a datatable that have many columns. When it displays data, the lines are applying wordwrap in some cells, because the grid is too wider to page. I'm...
4
by: the_mikado | last post by:
Hi, If this has been answered before I am sorry, but I want to wordwrap the items in a listbox so I dont have to use the horizontal scroll bar. Is this possible? if so how can it be done? ...
4
by: AlexBur | last post by:
Hi everybody. I learn C# several month only and sometimes have problems with it. I hope somebody will help me. When wordwrap is active and i add text to the TextBox i have strange result, some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.