|
How is the data being presented? Is it just one file/form entry in the format you've described? If it's a form, the 'title' and 'text' should be entered in separate input fields so you don't have to parse.
Otherwise, you have to be certain the data will always be provided in the format you've described ('title' followed by two CR/LF's, followed by 'text' followed by two CR/LF's. etc..) -- not a very standard protocol and if you have control over how the data is being provided, I suggest changing it.
If you're stuck with that though, you're going to need to maintain state, so you'll need a loop containing your string replacement function(s) or regular expression(s). First, I would replace translate any \r\n combos to just a plain old \n; secondly I would replace your double \n\n's with a single \n -- there is really no need once you get to the parsing this to have multiple CR/LF's. After that, loop through each line of text, assuming the first line is a title, the second your text, until the loop is complete.
And about storing the data in a database, I would not store the HTML content in the database. You may decide to change how the content is displayed sometime down the line. Let the script that displays the output control this. In this case, use your database for storing your data, not your markup.
|