By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

wordwrap, chunk_split or something else can help me?

P: 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>

Share this Question
Share on Google+
2 Replies


Atli
Expert 5K+
P: 5,058
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

P: 7
You're great! Thank you so much, i can resolve my probleme!
Dec 17 '09 #3

Post your reply

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