Hi,

I thought this would be a pretty simple thing to do, but I'm really having problems getting this wordwrap function to work.

I'm trying to get the word wrap function to work. I'm retrieving data from a mySQL database. The data has been base64_encoded

$wrapthis = base64_decode($data);
$output = wordwrap($wrapthis, 80, "<br />\n");

I'm putting the $output into a html table.

$wrapthis returns the data in the right text format however the wordwrap doesn't work. I'm thinking maybe the wordwrap function isn't recognizing the whitespaces between the words when they come out of a base64_decode??

What am I doing wrong???

Help!

    I tested your code.
    It works perfectly alright when I echo $output
    It even put <br />\n at the end of each line.

    My test

    <?php
    
    $str = "wrapthis returns the data in the right text format however the wordwrap doesn't work. I'm thinking maybe the wordwrap function isn't recognizing the whitespaces between the words when they come out of a base64_decode??";
    
    $data = base64_encode($str);
    
    $wrapthis = base64_decode($data);
    $output = wordwrap($wrapthis, 80, "<br />\n");
    echo $output;
    
    ?>

      I agree....the code works when I just run it in one instance without storing it in the sql database. I forgot to mention that I actually have a nl2br when I'm inserting the data in the database. So the code actually looks like this:

      $str = "wrapthis returns the data in the right text format however the wordwrap doesn't work. I'm thinking maybe the wordwrap function isn't recognizing the whitespaces between the words when they come out of a base64_decode??"; 
      
      $data = base64_encode(nl2br($str)); 
      
      $wrapthis = base64_decode($data); 
      $output = wordwrap($wrapthis, 50, "<br />\n"); 
      echo $output; 
      
      

      However this seems to wrap perfectly as well when run in one instance. Still the problem persists. Could it be something when the data is stored and retrieved?

        What HTML source output you get?

          the html is without the breaks.

          You can see the website here if you want to see what I'm talking about.

          http://forsoegsperson.dk/index.php

          It's in danish however but I echoet the two strings in the top of the page.

            Oh and just to show you the code I used:

            
            $wrapthis = base64_decode($string_from_sql);
            $intro = wordwrap($wrapthis, 30, "<br />\n"); 
            echo $intro."<br>";
            
            $str2 = "her er en introduktionstekst. du kan trykke pƄ linket nedenfor for at se hele annoncen. man kan skrive 500 tegn i introduktionen."; 
            $data2 = base64_encode(nl2br($str2)); 
            $wrapthis2 = base64_decode($data2); 
            $output2 = wordwrap($wrapthis2, 30, "<br />\n"); 
            
            echo $output2;
            

              Is there some point where you're replacing ' ' with '&nbsp;'? Or are you using '&#160;' ( a nonbreaking space, character 160) in the text? The first line of text on that page is full of '&nbsp;' and by definition the line won't be broken there.

                AHH! Of course! That actually solved the problem. Thanks a bunch. šŸ™‚

                The wordwrap is kinda working now however, there is another problem. I'm using this code

                wordwrap($wrapthis, 80, "<br />\n");

                I ran this on a some text from cnn.com and this it what it gives me in the html source:

                What this device does is extraordinary," Jobs said. "It is the best browsing<br /> 
                experience you've ever had. ... It's unbelievably great ... way better than a<br /> 
                laptop. Way better than a smartphone."<br /> 
                The computer will act as a sort of<br /> 
                missing link between the two. The model Jobs demonstrated at an invitation-only<br /> 
                event in San Francisco operated without a hardware keyboard, with Jobs typing on<br /> 
                what he described as a nearly full-size touchscreen keyboard.<br /> 
                "It's a<br /> 
                dream to type on," he said.<br /> 
                It has a</td> 
                

                So the wrap kinda works but some line are much smaller than others even though they are no where near the limit of 80. Any reason why it would do that??

                  My guess is that the text already has "<br />\n" at the end of each paragraph, and that is being counted with the characters in the next line to reach the line break count.

                  <?php
                  $text = "Line 1.<br />
                  this is line two blah blah blah yadda yadda yadda.";
                  echo "<pre>".htmlspecialchars(wordwrap($text, 35, "<br />\n"))."</pre>";
                  

                  Output:

                  Line 1.<br />
                  this is line two<br />
                  blah blah blah yadda yadda yadda.
                  

                    PS: Any particular reason you want to do this instead of using CSS to specify the desired width for the containing HTML element?

                      Well I guess you're right. I might as well just specify the width. That would be the easy way of doing it šŸ™‚

                      The reason I started to mess with the wordwrap function was because the string I retrieved from my database wouldn't wrap even if I speficied the width in the html element. Now that I have removed the non-breaking spaces this isn't happening anymore.

                      So I guess the wordwrap function is kinda redundant for me now.

                      Anyway, thanks to everyone for the help.

                      problem solved! 😃

                        Write a Reply...