Jump to content

PHP noob needs help with some string formatting.


ScoTTy_XY

Recommended Posts

Hey guys I was wondering if you could please help a noob learn abit more about php.

 

I have a mysql database with a text column in it. When browsed within the structure of the database the text is formatted as it was entered with spaces and new lines etc.

 

But when i retrieve the text into a php variable and try and display it within a paragraph all the text becomes one big block.

 

I was wondering if someone could please give me a lesson on retaining all the formatting in terms of spaces and new lines (enters). I want to display the text exactly as it is entered. Rather than compressed into one big paragraph.

 

Any help would be much appreciated.

 

Cheers guys

 

Dale

 

 

Link to comment
Share on other sites

This is something ive not long learnt but i know this code works for a fact as im using it right now. So i cant really explain it all, but hey it works!

 

Once you have registered you're text from the database for example

$text=mysql_result($result,$i,"text");[code] (You may have done this differently)

Then you need to use php explode to include line breaks. This code also includes a number to limit the amount of characters displayed, which is always useful. Just set it to your own needs as you can see mine is set at 1500 characters: [code]$text = wordwrap($text, 1500, "|", 1);
list($text) = explode('|', $text);

 

Then when you come to echo the text this is what you need to put

<? echo nl2br("$text") ?>

[/code][/code]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.