Questions | Login

Top / PHP

PHP convert line breaks to paragraph tags?

demo 22 Dec 2008 07:49pm
I have a form that submits data to a MySQL database and then I have a page that displays the submitted data. Some of the data is articles which means multiple paragraphs. Now I want text indents for each paragraph but I only know how to do those in HTML paragraph tags. Is there any PHP function or whatever that can do this for me? I already have 'nl2br', are there any others?

Answers

demo 22 Dec 2008 07:57pm
nl2br will probably do what you need, but if you need more you'll probably have to write a little parser.
demo 23 Dec 2008 02:23am
if "$txt" is your text: (ie bla-bla< rb />blabla< rb />)
[ I am using "rb" because this editor converts "b r" ! ]

$txt = str_replace ( "< rb / >", "< /p >< p >", $txt );
// replaces all br with "end para" "para" tags.
Then:
$txt = "< p > " . $txt; // to add the first one!
Finally:
$l = strlen ($txt) - 3; // 3 is the length of the last <p> you have added!
$txt = substr ($txt, 0, $l);

(You MAY have to review this if you stored into a "TEXT" type: there will be "invisible" rn you will also have to replace with < rb >!
$txt = str_replace ("rn", " < r b / > ", $txt);

Related

Submit Answer

You must be logged in to post an answer. Please Login or Register.

Powered by phpMyAnswers.