PHP/MySQL - Formatting parapgraphs

Associate
Joined
12 Aug 2004
Posts
1,009
Location
Glasgow, Scotland
Hi :)

...Having a bit of hascel with formatting paragraphs here in a content management system. I have an edit page area which requests the content from a database and puts it inside a textarea for a user to edit, but its coming up as normal html instead of the paragraphs being spaced out.

What I'm using is:

Code:
	//Convert Windows (\r\n) to Unix (\n)
	$PageContent = ereg_replace("\r\n", "\n", $PageContent);
	// Convert Macintosh (\r) to Unix (\n)
	$PageContent = ereg_replace("\r", "\n", $PageContent);
	// Handle paragraphs
	$PageContent = ereg_replace("\n\n", '</p><p>', $PageContent);
	// Handle line breaks
	$PageContent = ereg_replace("\n", '<br />', $PageContent);

which I have got for taking the data out of the database and correctly converting it into html for viewing on a normal page, but I'm trying to reverse it so that when it's taken out of the database instead of showing the html it puts the data as normal, editable paragraphs.

I tried simply switching "\r\n" with "\n" around and the rest of them and that sort of works but instead of having 1 line between each paragraph theres 5.

Is there anything else I can do?
 
I can't see why you need to do anything with it. If it's stored in the DB with linebreaks between 'paragraphs', when you pull it out of the DB and place it in the <textarea> the original linebreaks and formatting will remain.

For decent line-break to paragraphs conversion, check out autop (as used in Wordpress).
 
I thought that as well... but if I use it like the code appears in my first post it will return the data like below in the <textarea>

<p>The screened private pool (28ft. by 14ft.) looks onto a pine forest and has a covered lanai and extended sun deck with a range of luxury pool furniture and a gas barbecue. The sun deck faces South West and receives direct sunlight from around 8am until sundown. The pool can be heated if required.</p><p>The villa meets all fire and safety regulations and we have a friendly and helpful management company who are available 24 hours a day should the need arise. We have installed a high tech security entry system which means there is no need to call at our management company to collect the keys for the villa.</p><p>Should you require assistance travelling to the villa our management company can collect you from either Orlando International or Sanford airports. Our management company can also deliver groceries to the villa prior to your arrival. Should you require these additional services please ask about the costs involved when booking.</p><p>Calabay Parc is within 10 to 15 minutes of Walt Disney World and many other major Orlando attractions, including Sea World and Universal Studios. It is approximately one hour from the Kennedy Space Centre and within easy travelling distance of both the West and East Coasts with their miles of beautiful sandy beaches. Calabay Parc is also within a few minutes of a number of championship golf courses and some superb restaurants and fast food outlets</p>

That's understandable because if you look at the code it's telling the data to change from \n\n to </p><p>, so to solve this I tried turning the contents of the swap (\n\n & </p><p> around it sort of works except instead of having 1 line between parapragh it puts in 5.
 
Store it in the database as:

Code:
Foo foo foo foo, foo.

Bar bar foo bar foo.

On display, convert that to (using autop):

Code:
<p>Foo foo foo foo, foo.</p>

<p>Bar bar foo bar foo.</p>

And then when you edit it you don't have to do anything. The newlines will display as newlines in the textarea.
 
I tried using autop ($PageContent = autop($PageContent);) but it came back saying Fatal error: Call to undefined function: autop()
 
Back
Top Bottom